Home Php C# Sql C C++ Javascript Python Java Go Android Git Linux Asp.net Django .net Node.js Ios Xcode Cocoa Iphone Mysql Tomcat Mongodb Bash Objective-c Scala Visual-studio Apache Elasticsearch Jar Eclipse Jquery Ruby-on-rails Ruby Rubygems Android-studio Spring Lua Sqlite Emacs Ubuntu Perl Docker Swift Amazon-web-services Svn Html Ajax Xml Java-ee Maven Intellij-idea Rvm Macos Unix Css Ipad Postgresql Css3 Json Windows-server Vue.js Typescript Oracle Hibernate Internet-explorer Github Tensorflow Laravel Symfony Redis Html5 Google-app-engine Nginx Firefox Sqlalchemy Lucene Erlang Flask Vim Solr Webview Facebook Zend-framework Virtualenv Nosql Ide Twitter Safari Flutter Bundle Phonegap Centos Sphinx Actionscript Tornado Register | Login | Edit Tags | New Questions | 繁体 | 简体


10 questions online user: 36

0
votes
answers
24 views
+10

禁用文本字段編輯而不阻止清除按鈕

1

你知道我如何讓edittext對用戶不可編輯,但允許它使用文本字段自己的清除按鈕來刪除它嗎?禁用文本字段編輯而不阻止清除按鈕

我嘗試使用:

textField.isEnabled = false 

textField.isUserInteractionEnabled = false 

,而是因爲它禁止一切

沙发
0
1

返回false它沒有很好地工作文本字段委託的textFieldShouldBeginEditing

+0

非常感謝,非常感謝:D –

0
votes
answers
8 views
+10

斯威夫特4:數組索引範圍

-3

的我是新來的迅速,我有一個簡單的問題,看着對計算器的相關問題,他們都不在迅速4,所以我不得不問斯威夫特4:數組索引範圍

在這裏,我有一個數組,看起來像這樣的日誌

["72 Wilson St, Manchester M11 2AZ, UK"] // 0 
["Goldbergweg 117, 60599 Frankfurt am Main, Germany"] // 1 
["Rondweg Zuid, 3817 Amersfoort, Netherlands"] // 2 am i right? 

,我用這個代碼來獲取的元素在裏面:

var stringArray = [String]() 
stringArray.append(forss as! String) 
print(stringArray 
self.predit1.text = stringArray[2] 
self.predit2.text = stringArray[0] 
self.predit3.text = stringArray[1] 

但得到:

Thread 1: Fatal error: Index out of range

沙发
0
2

你申報的String

var stringArray = [String]() 

數組然後你追加一個元素

stringArray.append(forss as! String) 

這意味着只有指數0是有效的

stringArray[0] 

,你會得到了超範圍的錯誤

stringArray[1] 
stringArray[2] 
etc. 

注:

問題不在於特別是與雨燕雨燕4表現出該性能的所有版本。

+0

OMG什麼是一個簡單的答案,非常感謝你 –

板凳
0
0

就我在第一個代碼塊中看到的,看起來你有3個數組,每個數組只有一個字符串元素。 數組初始化應該是這樣的:

let stringArray = [ 
    "72 Wilson St, Manchester M11 2AZ, UK", // 0 
    "Goldbergweg 117, 60599 Frankfurt am Main, Germany", // 1 
    "Rondweg Zuid, 3817 Amersfoort, Netherlands" // 2 
] 
+0

嗨,我無法發佈完整的代碼,但stringArray會得到三次命中,它會返回隨機值,所以我不能使用你的 –

0
votes
answers
29 views
+10

Swift tableView滾動到行給出的內容的值不正確我的tableview的偏移值

0

在我的應用程序中,我有一個tableview,其中顯示約400個產品。我現在面臨的問題是,在滾動到部分的開始編程就像這樣:Swift tableView滾動到行給出的內容的值不正確我的tableview的偏移值

@objc func CategoryPress(_ button: UIButton) { 

    let indexPath = NSIndexPath(item: 0, section: button.tag) 
    tableView.scrollToRow(at: indexPath as IndexPath, at: UITableViewScrollPosition.top, animated: false) 

} 

我正在爲我的tableView.contentOffset.y一個錯誤的值,直到我達到我的tableview的頂部是不正確停留再次。 只有當滾動到遠離我的起始位置的位置時纔會發生此問題。

雖然手動滾動我不面臨這個問題,我所有的值都是確切的。

我試着滾動到按鈕按下特定的y,但我沒有找到辦法做到這一點。

任何解決方案或替代獲得特定位置將受到高度讚賞。

沙发
0
0

我發現了一個工作的替代它是:

@obj func CategoryPress(_ button: UIButton) { 

    let indexPath = NSIndexPath(item: 0, section: button.tag) 
    var offset: CGPoint = tableView.contentOffset 
    offset.y = CGFloat(Ycoordinates[indexPath.section]) 
    tableView.setContentOffset(offset, animated: true) 

} 
148
votes
answers
22 views
+10

How can I create local notifications in iOS?

I would like to know how I can setup local notifications so that at the time I set, my app generates a notification/alert with a customized message...

沙发
+890

以下是適用於我的項目的 LocalNotification 的示例代碼。

Objective-C:

此代碼塊在<代碼>的AppDelegate 文件:

 <代碼>  - (BOOL)應用:(UIApplication的*)應用didFinishLaunchingWithOptions:(NSDictionary的*)launchOptions {[launchOptions valueForKey:UIApplicationLaunchOptionsLocalNotificationKey]; //在應用程序啟動後覆蓋自定義點。返回YES; //當應用程序處於前台(活動模式)時調用此代碼塊 - (void)應用程序:(UIApplication *)應用程序didReceiveLocalNotification:(UILocalNotification *)通知{UIAlertView * notificationAlert = [[UIAlertView alloc] initWithTitle:@“ soundName = UILocalNotificationDefaultSoundName; notification.applicationIconBadgeNumber = 10; [[UIApplication sharedApplication] scheduleLocalNotification:notification]; }   

上面的代碼顯示一個AlertView後7秒的時間間隔按下按鈕綁定 startLocalNotification 如果應用程序在後台然後顯示 BadgeNumber 為10並具有默認通知聲音。

此代碼適用於iOS 7.x及更低版本,但對於 iOS 8,它將在控制台上提示:< / p>

嘗試使用警報安排本地通知但未收到用戶顯示警報的權限

這意味著您需要註冊本地通知。這可以使用以下方法實現:

  if([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings :)]){[application registerUserNotificationSettings [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]]; }   

你也可以參考博客,用於本地通知。

Swift:

AppDelegate.swift 文件應該看起來像這個:

  func應用程序(應用程序:UIApplication,didFinishLaunchingWithOptions launchOptions:[NSObject:AnyObject]?) - &gt; Bool {//在應用程序啟動後覆蓋自定義點。application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes:UIUserNotificationType.Sound | UIUserNotificationType.Badge | UIUserNotificationType.Alert,類別:無))返回true}   

在迅速文件(比如<代碼> ViewController.swift )你要在其中創建本地通知應包含以下代碼:

  // MARK: - 按鈕函數func buttonIsPressed(sender:UIButton){println(“buttonIsPressed函數調用( UIButton.description())“)var localNotification = UILocalNotification()localNotification.fireDate = NSDate(timeIntervalSinceNow:3)localNotification.alertBody =“這是從2.0斯威夫特本地通知”localNotification.timeZone = NSTimeZone.localTimeZone()localNotification.repeatInterval = NSCalendarUnit.CalendarUnitMinute localNotification.userInfo = [“重要”:“數據”]; localNotification.soundName = UILocalNotificationDefaultSoundName localNotification.applicationIconBadgeNumber = 5 localNotification.category =“Message”UIApplication.sharedApplication()。scheduleLocalNotification(localNotification)} // MARK: -  viewDidLoad class ViewController:UIViewController {var objButton:UIButton!override func viewDidLoad(){super.viewDidLoad()。objButton = UIButton.buttonWithType(.Custom)為?UIButton objButton.frame = CGRectMake(30,100,150,40)objButton.setTitle(“點擊我”,forState:。正常)objButton.setTitle(“按下按鈕”,forState:.Highlighted)objButton.addTarget(個體,動作:“buttonIsPressed:”,forControlEvents:.TouchDown) 。}。}   

您在iOS 9及更低版本中使用本地通知的方式在iOS 10中完全不同。

在屏幕抓取下方來自Apple的發行說明描述了這一點。

您可以參考UserNotification的rel =“noreferrer”> Apple參考文檔

以下是本地通知的代碼:

Objective-C :

  1. App-delegate.h 文件中使用 @import UserNotifications;

  2. App-delegate應符合 UNUserNotificationCenterDelegate 協議

  3. didFinishLaunchingOptions 中使用以下代碼:

      UNUserNotificationCenter * center = [UNUserNotificationCenter currentNotificationCenter]; [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert)completionHandler:^(BOOL授予,NSError * _Nullable錯誤){if(!error){NSLog(@“request authorization succeeded!”); [self showAlert]; }};  - (void)showAlert {UIAlertController * objAlertController = [UIAlertController alertControllerWithTitle:@“Alert”消息:@“顯示警告!” preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@“OK”樣式:UIAlertActionStyleCancel處理程序:^(UIAlertAction * action){NSLog(@“Ok clicked!”); }]; [objAlertController addAction:cancelAction]; [[[[[UIApplication sharedApplication] windows] objectAtIndex:0] rootViewController] presentViewController:objAlertController animated:YES completion:^ {}];    
  4. 現在在任何視圖控制器中創建一個按鈕,在IBAction中使用以下代碼:

      UNMutableNotificationContent * objNotificationContent = [[ UNMutableNotificationContent alloc] init]; objNotificationContent.title = [NSString localizedUserNotificationStringForKey:@“Notification!”參數:nil]; objNotificationContent.body = [NSString localizedUserNotificationStringForKey:@“這是本地通知消息!”參數:nil]; objNotificationContent.sound = [UNNotificationSound defaultSound]; // 4.更新應用程序圖標徽章編號objNotificationContent.badge = @([[UIApplication sharedApplication] applicationIconBadgeNumber] + 1); //在五秒鐘內發送通知。UNTimeIntervalNotificationTrigger * trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:10.f重複:否]; UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:@“ten”content:objNotificationContent trigger:trigger]; // 3.安排localNotification UNUserNotificationCenter * center = [UNUserNotificationCenter currentNotificationCenter]; [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error){if(!error){NSLog(@“Local Notification succeeded”); } else {NSLog(@“本地通知失敗”); }};     

    Swift 3:

    1. AppDelegate.swift 文件中使用 import UserNotifications
    2. Appdelegate應符合 UNUserNotificationCenterDelegate protocol
    3. didFinishLaunchingWithOptions 中使用以下代碼

        //在應用程序啟動後覆蓋自定義點。let center = UNUserNotificationCenter.current()center.requestAuthorization(options:[.alert,.sound]){(授予,錯誤)//根據授權啟用或禁用功能。if error!= nil {print(“Request authorization failed!”)} else {print(“Request authorization succeeded!”)self。
           
      			
              

我是否一定需要在按下按鈕時運行funcButtonIsPressed?如果我希望應用程序默認情況下每週發出一次通知,我應該將它添加到初始VC的viewDidLoad中,該怎麼辦? - Dave G 16年2月24日1:08

另外,為什麼你的AppDelegate.swift文件有兩次didFinishLaunchingWithOptions? - Dave G 2016年2月24日10:32

“導入UserNotifications”在ViewController中導入 - iOS 1月2日11:05

板凳
+510

在appdelegate.m文件中,在applicationDidEnterBackground中編寫以下代碼以獲取本地通知

   - (void)applicationDidEnterBackground:(UIApplication *)application {UILocalNotification * notification = [[UILocalNotification alloc]在裡面]; notification.repeatInterval = NSDayCalendarUnit; [notification setAlertBody:@“Hello world”]; [notification setFireDate:[NSDate dateWithTimeIntervalSinceNow:1]]; [notification setTimeZone:[NSTimeZone defaultTimeZone]]; [application setScheduledLocalNotifications:[NSArray arrayWithObject:notification]]; }  
     
			
        

謝謝。幫幫我:D - 弗拉基米爾2014年4月28日7:01

當您使用setScheduledLocalNotifications安排一個通知時:是不必要的。有scheduleLocalNotification方法,它接受一個參數 - 要安排通知。developer.apple.com.cn/library/ios/DOCUMENTATION/UIKit/Reference / ...: - Paul Brewczynski 2014年9月25日15:28

地板
+60

創建本地通知非常簡單。只需按照以下步驟操作

  1. 在viewDidLoad()函數中,詢問用戶是否允許您的應用顯示通知。為此,我們可以使用以下代碼。

      UNUserNotificationCenter.current()。requestAuthorization(options:[.alert,.sound,.badge],completionHandler:{didAllow,error in})   
  2. 然後您可以創建一個按鈕,然後在action函數中編寫以下代碼以顯示通知。

      //創建通知內容let content = UNMutableNotificationContent()//添加標題,副標題,正文和徽章content.title =“嘿這是簡化的iOS”content.subtitle =“ 即使應用程序位於前台,您也可以顯示通知。為此,您只需添加名為 UNUserNotificationCenterDelegate     

    的委託。有關詳細信息,請訪問此博客文章: iOS本地通知教程

是否可以每天用太平洋時間重複通知並重複直到應用程序打開? - Mitul Marsoniya 18年2月23日凌晨5點

@KashfaKhan你能告訴我,當應用程序處於後台並且應用程序收到通知時,會執行哪種方法? - ArgaPK 18年8月18日11:49

4楼
+10
   - (void)applicationDidEnterBackground:(UIApplication *)application {UILocalNotification * notification = [[UILocalNotification alloc] init]; notification.repeatInterval = NSDayCalendarUnit; [notification setAlertBody:@“Hello world”]; [notification setFireDate:[NSDate dateWithTimeIntervalSinceNow:1]]; [notification setTimeZone:[NSTimeZone defaultTimeZone]]; [application setScheduledLocalNotifications:[NSArray arrayWithObject:notification]]; }   

這是有效的,但在iOS 8.0及更高版本中,您的應用程序必須使用註冊用戶通知 - [UIApplication registerUserNotificationSettings:] 在能夠安排和呈現UILocalNotifications之前,不要忘記這一點。

- [UIApplication registerUserNotificationSettings:]將覆蓋推送通知設置。所以如果使用了push actionable通知,請注意。 - Avijit Nagare 2016年7月11日9:07

5楼
+10
UIApplicationDelegate,UNUserNotificationCenterDelegate {let notificationCenter = UNUserNotificationCenter.current()var window:UIWindow?func application(_ application:UIApplication,didFinishLaunchingWithOptions launchOptions:[UIApplication.LaunchOptionsKey:Any]?) - &gt; Bool {//確認Delegete並請求權限notificationCenter.delegate = self let options:UNAuthorizationOptions = [.alert,.sound,.badge] notificationCenter.requestAuthorization(options:options){(didAllow,error)in if!didAllow {print (“用戶已拒絕通知”)}}返回true} func applicationWillResignActive(_ application:UIApplication){} func applicationDidEnterBackground(_ application:categoryIdentifier = categoryIdentifire //添加帶有更多內容的通知附件if(notificationType ==“帶內容的本地通知”){let imageName =“Apple”guard let imageURL = Bundle.main.url(forResource:imageName,withExtension:“png” )else {return} let attachment = try!UNNotificationAttachment(標識符:imageName,url:imageURL,選項:.none)content.attachments = [attachment]} let trigger = UNTimeIntervalNotificationTrigger(timeInterval:5,重複:false)let identifier =“Local Notification”let request = UNNotificationRequest(identifier:標識符,內容:內容,觸發器:觸發器)notificationCenter。count} func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath) - &gt; UITableViewCell {let cell = tableView.dequeueReusableCell(withIdentifier:“Cell”,for:indexPath)cell.textLabel?.text = notifications [indexPath.row] return cell} func tableView(_ tableView:UITableView,didSelectRowAt indexPath:IndexPath){let notificationType = notifications [indexPath.row] let alert = UIAlertController(標題:“”,消息:“5秒後”+ notificationType +“將出現”,preferredStyle:.alert)讓okAction = UIAlertAction(標題:“好的,我會的在self.appDelegate中等待“,style:.default”{(action)。

這對我有用...謝謝swifyBoy - Khushal iOS Jun 12 at 12:11

6楼
0

iOS 8及以上用戶,請將其包含在App委託中以使其正常工作。

   - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings :)]){[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]]; } return YES; }   

然後添加這行代碼會有所幫助,

   - (void)applicationDidEnterBackground:(UIApplication *)application {UILocalNotification * notification = [[ UILocalNotification alloc] init]; 通知。repeatInterval = NSDayCalendarUnit; [notification setAlertBody:@“Hello world”]; [notification setFireDate:[NSDate dateWithTimeIntervalSinceNow:1]]; [notification setTimeZone:[NSTimeZone defaultTimeZone]]; [application setScheduledLocalNotifications:[NSArray arrayWithObject:notification]]; }  
     
			
        
7楼
0
   - (void)kundanselect {NSMutableArray * allControllers = [[NSMutableArray alloc] initWithArray:self.navigationController.viewControllers]; NSArray * allControllersCopy = [allControllers copy]; if([[allControllersCopy lastObject] isKindOfClass:[kundanViewController class]]){[[NSNotificationCenter defaultCenter] postNotificationName:@“kundanViewControllerHide”object:nil userInfo:nil]; } else {[[NSUserDefaults standardUserDefaults] setInteger:4 forKey:@“selected”]; [self performSegueWithIdentifier:@“kundansegue”發件人:self]; }   

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ApparelsViewControllerHide)name:@“ApparelsViewControllerHide”對象:

0
votes
answers
5 views
+10

如何解決Swift中的「預期聲明」錯誤?

-1

我有點誤解如何正確使用 - (void)。每當我嘗試使用 - (Void)編寫代碼,但出現錯誤「Expected declaration」或有時「Binary operator '-' cannot be applied to operands of type 'UIFont' and '(Void).Type'」。如何解決Swift中的「預期聲明」錯誤?

這是怎麼發生的?在swift中正確使用此功能的正確方法是什麼?

{

- (Void)keyboardWillShow { //>> showing me the error "Binary operator '-' cannot be applied to operands of type 'UIFont' and '(Void).Type'" 
     // Animate the current view out of the way 
     if (self.view.frame.origin.y >= 0) 
     { 
      [self setViewMovedUp:YES]; 
     } 
     else if (self.view.frame.origin.y < 0) 
     { 
      [self setViewMovedUp:NO]; 
     } 
    } 

}

感謝

+1

請通讀了雨燕編程語言[Apple的文檔(https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/index.html)。 –

+1

用導致問題的實際代碼更新您的問題。 – rmaddy

+1

這是Objective-C代碼,而不是Swift代碼。請閱讀[Swift編程語言](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/)書。 – rmaddy

沙发
0
5

這聽起來像你想聲明沒有返回值的函數。在斯威夫特這樣做是因爲:

func thisFunction() { 
    .. 
} 
板凳
0
-2

可以回到零值,如下面的代碼:

func myFunc(myVar: String) ->()? { 
    print(myVar) 
    return nil 
    } 
var c = myFunc("Hello") 
print(c) // nil 

但斯威夫特功能總是返回默認值零,如果沒有聲明。如下面的代碼:

func myFunc(myVar: String) { 
     print(myVar) 
     return 
     } 
    var c = myFunc("Hello") 
    print(c) //() 
+0

* Swift函數總是返回默認值nil,如果沒有聲明*是錯誤的。在第二個例子中,你會得到一個編譯器錯誤(實際上兩個在Swift 3+中)。 – vadian

+1

*「如果未聲明,Swift函數總是返回默認值nil」* - 否,這是不正確的。 – rmaddy

+0

嚴格地說,即使沒有定義返回值,myFunc(myVar :)函數的這個版本仍然會返回一個值。沒有定義返回類型的函數返回一個特殊的Void類型值。這只是一個空元組,寫成()。 –

0
votes
answers
10 views
+10

swiftc不產生輸出並且沒有錯誤

1

我有一個簡單的問題:swiftc不產生輸出並且沒有錯誤

迅速程序

print("Hello World") 

與命令編譯:

swiftc -o test test.swift 

不產生輸出(無可執行文件)和沒有錯誤。

Swift版本是4.0.2,安裝在Xubuntu 17.10的$HOME/opt/swift/usr上。

我已經安裝了clanglibicu-dev並應用了以下補丁來/etc/environment

PATH=/home/[username]/opt/swift/usr/bin:[usual PATH] 
export C_INCLUDE_PATH=/home/[username]/opt/swift/usr/lib/swift/clang/include 
export CPLUS_INCLUDE_PATH=$C_INCLUDE_PATH 
+0

只是爲了確保:編譯完成後,你執行了*程序嗎? –

+0

沒有。它沒有給我一個可執行文件。那就是問題所在 – JPSgfx

沙发
0
1

好吧,我只是愚蠢。 有一點我必須卸載鐺。重新安裝它修復了一切。雖然swiftc可以告訴我一些錯誤信息中的東西...

917
votes
answers
13 views
+10

How to control the line spacing in UILabel

Is it possible to reduce the gap between text when put in multiple lines in a UILabel? We can set the frame, font size and # of lines. I want to reduce the gap between the two lines in that label.

up vote 236 down vote accepted favorite
沙发
+2360
+50

I thought about adding something new to this answer, so I don't feel as bad... Here is a Swift answer:

import Cocoa

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 40

let attrString = NSMutableAttributedString(string: "Swift Answer")
attrString.addAttribute(.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attrString.length))

var tableViewCell = NSTableCellView()
tableViewCell.textField.attributedStringValue = attrString

"Short answer: you can't. To change the spacing between lines of text, you will have to subclass UILabel and roll your own drawTextInRect, or create multiple labels."

See: Set UILabel line spacing


This is a really old answer, and other have already addded the new and better way to handle this.. Please see the up to date answers provided below.

從iOS 6.0開始,您可以通過NSAttributedString控制它(在Xcode的界面構建器中也可以在UILable的屬性中使用)。 - ıɾuǝʞ12年12月18日9:21

有趣的是,就我所知,你可以在行之間添加額外的間距,但在使用NSAttributedString時不能通過NSParagraphStyle減少它。(我可能需要對其他可修改屬性進行更多測試,但lineSpacing屬性只允許你增加它。) - livingtech 2013年9月12日21:28

看到我的回答,看看使用NSAttributedString的方法 - d.ennis 2013年10月8日21:21

@livingtech這真氣,我相信你是對的。你找到了解決辦法嗎? - Dom Vinyard 2014年11月7日14:04

只是為了澄清這個帖子中的內容。如果你想縮小行間距,將行高設置為1.0,然後將setLineHeightMultiple設置為較低的值<1.0,如:[paragraphStyle setLineHeightMultiple:0.8]或paragraphStyle.lineHeightMultiple = 0.8 - virsunen 2016年7月13日2:25

+3840

In Xcode 6 you can do this in the storyboard:

enter image description here

這應該有更多的讚成! - Nic Hubbard於2014年12月5日3:23

@PaperThick在6.1.1中有同樣的問題。它“哈利震動”幾分鐘。不知道如何解決它:) Xcode Shaking - Anton Gaenko 2015年1月16日12:15

有沒有辦法以這種方式設置自定義字體?我似乎無法改變任何其他字體的helvetica neue。 - Marcos Curvello 2015年2月25日1:57

如果你啟用'Attributed',然後打開文件作為源代碼,你可以手動編輯'lineHeightMultiple',因此繞過Harlem Shake bug - ED-209 2015年6月4日14:25

@azdev對於那些仍然在看這個的人,我在Xcode 7.3中得到了更長的震撼,但我認為這是第一個沒有出現問題的版本 - LulzCow 16年4月13日在20:07

+1000

Starting from iOS 6 you can set an attributed string to the UILabel. Check the following :

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:label.text];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = spacing;
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, label.text.length)];

label.attributedText = attributedString;

attributionString必須是NSMutableAttributedString(NOT NSAttributedString) - Mike S 2014年9月25日17:01

第一行代碼應該是NSMutableAttributedString * attributedString = [NSMutableAttributedString alloc] initWithString:@“sample text”]; - 艾倫12年12月12日15:11

NSMutableParagraphStyle的lineSpacing屬性從不為負數,因此使用此方法無法減小行高。要回答這個問題,你必須使用另一個屬性,請參閱@ d.ennis回答。 - Theo 2015年5月3日10:28

+790

The solutions stated here didn't work for me. I found a slightly different way to do it with the iOS 6 NSAttributeString:

myLabel.numberOfLines = 0; 
NSString* string = @"String with line one. 
 Line two. 
 Line three.";
NSMutableParagraphStyle *style  = [[NSMutableParagraphStyle alloc] init];
style.minimumLineHeight = 30.f;
style.maximumLineHeight = 30.f;
NSDictionary *attributtes = @{NSParagraphStyleAttributeName : style,};
myLabel.attributedText = [[NSAttributedString alloc] initWithString:string
                                                         attributes:attributtes];   
[myLabel sizeToFit];

行高與字體大小有關。行間距就是行間距。您可以通過設置最小/最大行高來解決問題,但這只是因為您使用的當前字體大小不大於行高邊界。根據文檔:“......超過此高度的字形和圖形將與相鄰的線重疊......雖然此限制適用於線條本身,但線間距會在相鄰線條之間增加額外的空間。” - Ari Braginsky 2013年10月17日1:11

+1,如果你想減少線之間的間距,這就是你想要做的。實際行間距很可能默認為0,這就是為什麼人們報告你只能增加它。間距太大的問題來自於線高太大,這就是為什麼這將在99%的時間內完成工作。 - lawicko 2015年3月10日11:11

這是我能找到的唯一一個使用Photoshop,Sketch,CSS等設計應用程序常用的行高度值(而不是比率)的答案.-- Albert Bori 17年1月24日19:16

+310

I've made this simple extension that works very well for me:

extension UILabel {
    func setLineHeight(lineHeight: CGFloat) {
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = 1.0
        paragraphStyle.lineHeightMultiple = lineHeight
        paragraphStyle.alignment = self.textAlignment

        let attrString = NSMutableAttributedString()
        if (self.attributedText != nil) {
            attrString.append( self.attributedText!)
        } else {
            attrString.append( NSMutableAttributedString(string: self.text!))
            attrString.addAttribute(NSAttributedStringKey.font, value: self.font, range: NSMakeRange(0, attrString.length))
        }
        attrString.addAttribute(NSAttributedStringKey.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attrString.length))
        self.attributedText = attrString
    }
}

Copy this in a file, so then you can use it like this

myLabel.setLineHeight(0.7)

記住如果你在使用Storyboard這個標籤時這樣做,那麼一定要把你的標籤線設置為0 - Honey 2016年8月1日3:10

你為什麼不直接設置lineSpacing而忘記設置lineHeightMultiple? - 蜂蜜2016年8月1日3:27

因為減少線高的關鍵是'lineHeightMultiple',沒有lineSpacing - Agustin Meriles 2016年8月1日3:33

假設你想要你的行高為1.4,為什麼你不能只寫.lineSpacing = 1.4而忘記了所有關於.lineHeightMultiple ... - Honey 2016年8月1日在3:40

你試過嗎?它有用嗎? - 奧古斯丁梅里斯2016年8月1日3:42

+230

From Interface Builder (Storyboard/XIB):

enter image description here

Programmatically:

SWift 4

Using label extension

extension UILabel {

    // Pass value for any one of both parameters and see result
    func setLineSpacing(lineSpacing: CGFloat = 0.0, lineHeightMultiple: CGFloat = 0.0) {

        guard let labelText = self.text else { return }

        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = lineSpacing
        paragraphStyle.lineHeightMultiple = lineHeightMultiple

        let attributedString:NSMutableAttributedString
        if let labelattributedText = self.attributedText {
            attributedString = NSMutableAttributedString(attributedString: labelattributedText)
        } else {
            attributedString = NSMutableAttributedString(string: labelText)
        }

        // Line spacing attribute
        attributedString.addAttribute(NSAttributedStringKey.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attributedString.length))

        self.attributedText = attributedString
    }
}

Now call extension function

let label = UILabel()
let stringValue = "How to
control
the
line spacing
in UILabel"

// Pass value for any one argument - lineSpacing or lineHeightMultiple
label.setLineSpacing(lineSpacing: 2.0) .  // try values 1.0 to 5.0

// or try lineHeightMultiple
//label.setLineSpacing(lineHeightMultiple = 2.0) // try values 0.5 to 2.0

Or using label instance (Just copy & execute this code to see result)

let label = UILabel()
let stringValue = "How to
control
the
line spacing
in UILabel"
let attrString = NSMutableAttributedString(string: stringValue)
var style = NSMutableParagraphStyle()
style.lineSpacing = 24 // change line spacing between paragraph like 36 or 48
style.minimumLineHeight = 20 // change line spacing between each line like 30 or 40

// Line spacing attribute
attrString.addAttribute(NSAttributedStringKey.paragraphStyle, value: style, range: NSRange(location: 0, length: stringValue.characters.count))

// Character spacing attribute
attrString.addAttribute(NSAttributedStringKey.kern, value: 2, range: NSMakeRange(0, attrString.length))

label.attributedText = attrString

Swift 3

let label = UILabel()
let stringValue = "How to
control
the
line spacing
in UILabel"
let attrString = NSMutableAttributedString(string: stringValue)
var style = NSMutableParagraphStyle()
style.lineSpacing = 24 // change line spacing between paragraph like 36 or 48
style.minimumLineHeight = 20 // change line spacing between each line like 30 or 40
attrString.addAttribute(NSParagraphStyleAttributeName, value: style, range: NSRange(location: 0, length: stringValue.characters.count))
label.attributedText = attrString

應該接受答案。 - 評論時間70年01月01日20:02

添加“paragraphStyle.alignment = self.textAlignment”行以保持原始對齊。否則,文本將保持對齊。 - Nithin Michael 4月29日9:57

+190

There's an alternative answer now in iOS 6, which is to set attributedText on the label, using an NSAttributedString with the appropriate paragraph styles. See this stack overflow answer for details on line height with NSAttributedString:

Core Text - NSAttributedString line height done right?

+160

Here is a class that subclass UILabel to have line-height property : https://github.com/LemonCake/MSLabel

這對我有用,謝謝。我也試過使用MTLabel,但這個更好。 - wzbozon 2013年1月10日16:20

有誰知道MSLabel是否支持''字符? - achi 2013年7月10日2:02

根據文檔,換行符被忽略 - Zee Dec 14 '14 at 11:11

+80

In Swift and as a function, inspired by DarkDust

// Usage: setTextWithLineSpacing(myEpicUILabel,text:"Hello",lineSpacing:20)
func setTextWithLineSpacing(label:UILabel,text:String,lineSpacing:CGFloat)
{
    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.lineSpacing = lineSpacing

    let attrString = NSMutableAttributedString(string: text)
    attrString.addAttribute(NSAttributedString.Key.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attrString.length))

    label.attributedText = attrString
}
+60

According @Mike 's Answer, reducing the lineHeightMultiple is the key point. Example below, it work well for me:

    NSString* text = label.text;
    CGFloat textWidth = [text sizeWithAttributes:@{NSFontAttributeName: label.font}].width;
    if (textWidth > label.frame.size.width) {
        NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
        paragraph.alignment = NSTextAlignmentCenter;
        paragraph.lineSpacing = 1.0f;
        paragraph.lineHeightMultiple = 0.75;     // Reduce this value !!!
        NSMutableAttributedString* attrText = [[NSMutableAttributedString alloc] initWithString:text];
        [attrText addAttribute:NSParagraphStyleAttributeName value:paragraph range:NSMakeRange(0, text.length)];
        label.attributedText = attrText;
    }
+50

SWIFT 3 useful extension for set space between lines more easily :)

extension UILabel
{
    func setLineHeight(lineHeight: CGFloat)
    {
        let text = self.text
        if let text = text 
        {

            let attributeString = NSMutableAttributedString(string: text)
            let style = NSMutableParagraphStyle()

           style.lineSpacing = lineHeight
           attributeString.addAttribute(NSParagraphStyleAttributeName,
                                        value: style,
                                        range: NSMakeRange(0, text.characters.count))

           self.attributedText = attributeString
        }
    }
}
+30

I've found a way where you can set the real line height (not a factor) and it even renders live in Interface Builder. Just follow the instructions below. Code is written in Swift 4.


Step #1: Create a file named DesignableLabel.swift and insert the following code:

import UIKit

@IBDesignable
class DesignableLabel: UILabel {
    @IBInspectable var lineHeight: CGFloat = 20 {
        didSet {
            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.minimumLineHeight = lineHeight
            paragraphStyle.maximumLineHeight = lineHeight
            paragraphStyle.alignment = self.textAlignment

            let attrString = NSMutableAttributedString(string: text!)
            attrString.addAttribute(NSAttributedStringKey.font, value: font, range: NSRange(location: 0, length: attrString.length))
            attrString.addAttribute(NSAttributedStringKey.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: attrString.length))
            attributedText = attrString
        }
    }
}

Step #2: Place a UILabel into a Storyboard/XIB and set its class to DesignableLabel. Wait for your project to build (build must succeed!).

Specifying the class to your UILabel


Step 3: Now you should see a new property in the properties pane named "Line Height". Just set the value you like and you should see the results immediately!

Set Line Height in properties

+10

In Swift 2.0...

Add an extension:

extension UIView {
    func attributesWithLineHeight(font: String, color: UIColor, fontSize: CGFloat, kern: Double, lineHeightMultiple: CGFloat) -> [String: NSObject] {
        let titleParagraphStyle = NSMutableParagraphStyle()
        titleParagraphStyle.lineHeightMultiple = lineHeightMultiple

        let attribute = [
            NSForegroundColorAttributeName: color,
            NSKernAttributeName: kern,
            NSFontAttributeName : UIFont(name: font, size: fontSize)!,
            NSParagraphStyleAttributeName: titleParagraphStyle
        ]
        return attribute
    }
}

Now, just set your UILabel as attributedText:

self.label.attributedText = NSMutableAttributedString(string: "SwiftExample", attributes: attributesWithLineHeight("SourceSans-Regular", color: UIColor.whiteColor(), fontSize: 20, kern: 2.0, lineHeightMultiple: 0.5))    

Obviously, I added a bunch of parameters that you may not need. Play around -- feel free to rewrite the method -- I was looking for this on a bunch of different answers so figured I'd post the whole extension in case it helps someone out there... -rab

+10

Swift3 - In a UITextView or UILabel extension, add this function:

I added some code to keep the current attributed text if you are already using attributed strings with the view (instead of overwriting them).

func setLineHeight(_ lineHeight: CGFloat) {
    guard let text = self.text, let font = self.font else { return }

    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.lineSpacing = 1.0
    paragraphStyle.lineHeightMultiple = lineHeight
    paragraphStyle.alignment = self.textAlignment

    var attrString:NSMutableAttributedString
    if let attributed = self.attributedText {
        attrString = NSMutableAttributedString(attributedString: attributed)
    } else {
        attrString = NSMutableAttributedString(string: text)
        attrString.addAttribute(NSFontAttributeName, value: font, range: NSMakeRange(0, attrString.length))
    }
    attrString.addAttribute(NSParagraphStyleAttributeName, value:paragraphStyle, range:NSMakeRange(0, attrString.length))
    self.attributedText = attrString
}
+10

Another answer... If you're passing the string programmatically, you need to pass a attributed string instead a regular string and change it's style.(iOS10)

NSMutableAttributedString * attrString = [[NSMutableAttributedString alloc] initWithString:@"Your 
regular 
string"];
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
[style setLineSpacing:4];
[attrString addAttribute:NSParagraphStyleAttributeName
                   value:style
                   range:NSMakeRange(0, attrString.length)];
_label.attributedText = attrString;
+10

Swift 3 extension:

    import UIKit

extension UILabel {
    func setTextWithLineSpacing(text: String, lineHeightMultiply: CGFloat = 1.3) {
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineHeightMultiple = lineHeightMultiply
        paragraphStyle.alignment = .center
        let attributedString = NSMutableAttributedString(string: text)
        attributedString.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSRange(location: 0, length: attributedString.length))
        self.attributedText = attributedString
    }
}
+10

This should help with it. You can then assign your label to this custom class within the storyboard and use it's parameters directly within the properties:

open class SpacingLabel : UILabel {

    @IBInspectable open var lineHeight:CGFloat = 1 {
        didSet {
            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.lineSpacing = 1.0
            paragraphStyle.lineHeightMultiple = self.lineHeight
            paragraphStyle.alignment = self.textAlignment

            let attrString = NSMutableAttributedString(string: self.text!)
            attrString.addAttribute(NSAttributedStringKey.font, value: self.font, range: NSMakeRange(0, attrString.length))
            attrString.addAttribute(NSAttributedStringKey.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attrString.length))
            self.attributedText = attrString
        }
    } 
}

這應該有幫助。然後,您可以將標籤分配給故事板中的此自定義類,並直接在屬性中使用它的參數。 - 羅素沃里克17年11月3日14:59

請不要在評論中提供與答案相關的內容。你的答案應該是有幫助的,而不必閱讀評論 - 孤獨的神經元17年11月3日在15:23

+10

Here is a subclass of UILabel that sets lineHeightMultiple and makes sure the intrinsic height is large enough to not cut off text.

@IBDesignable
class Label: UILabel {
    override var intrinsicContentSize: CGSize {
        var size = super.intrinsicContentSize
        let padding = (1.0 - lineHeightMultiple) * font.pointSize
        size.height += padding
        return size
    }

    override var text: String? {
        didSet {
            updateAttributedText()
        }
    }

    @IBInspectable var lineHeightMultiple: CGFloat = 1.0 {
        didSet {
            updateAttributedText()
        }
    }

    private func updateAttributedText() {
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineHeightMultiple = lineHeightMultiple
        attributedText = NSAttributedString(string: text ?? "", attributes: [
            .font: font,
            .paragraphStyle: paragraphStyle,
            .foregroundColor: textColor
        ])
        invalidateIntrinsicContentSize()
    }
}
+10

Swift 4 label extension. Creating NSMutableAttributedString before passing into function in case there are extra attributes required for the attributed text.

extension UILabel {

    func setLineHeightMultiple(to height: CGFloat, withAttributedText attributedText: NSMutableAttributedString) {

        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = 1.0
        paragraphStyle.lineHeightMultiple = height
        paragraphStyle.alignment = textAlignment

        attributedText.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: attributedText.length - 1))

        self.attributedText = attributedText
    }
}
0

This code worked for me (ios 7 & ios 8 for sure).

_label.numberOfLines=2;
_label.textColor=[UIColor whiteColor];

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineHeightMultiple=0.5;
paragraphStyle.alignment = NSTextAlignmentCenter;
paragraphStyle.lineSpacing = 1.0;

NSDictionary *nameAttributes=@{
                               NSParagraphStyleAttributeName : paragraphStyle,
                               NSBaselineOffsetAttributeName:@2.0
                               };


NSAttributedString *string=[[NSAttributedString alloc] initWithString:@"22m
ago" attributes:nameAttributes];
_label.attributedText=string;
0

Here is my solution in swift. The subclass should work for both attributedText and text property and for characterSpacing + lineSpacing. It retains the spacing if a new string or attributedString is set.

open class UHBCustomLabel : UILabel {
    @IBInspectable open var characterSpacing:CGFloat = 1 {
        didSet {
            updateWithSpacing()
        }

    }
    @IBInspectable open var lines_spacing:CGFloat = -1 {
        didSet {
            updateWithSpacing()
        }

    }
    open override var text: String? {
        set {
            super.text = newValue
            updateWithSpacing()
        }
        get {
            return super.text
        }
    }
    open override var attributedText: NSAttributedString? {
        set {
            super.attributedText = newValue
            updateWithSpacing() 
        }
        get {
            return super.attributedText
        }
    }
    func updateWithSpacing() {
        let attributedString = self.attributedText == nil ? NSMutableAttributedString(string: self.text ?? "") : NSMutableAttributedString(attributedString: attributedText!)
        attributedString.addAttribute(NSKernAttributeName, value: self.characterSpacing, range: NSRange(location: 0, length: attributedString.length))
        if lines_spacing >= 0 {
            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.lineSpacing = lines_spacing
            paragraphStyle.alignment = textAlignment
            attributedString.addAttribute(NSParagraphStyleAttributeName, value:paragraphStyle, range:NSMakeRange(0, attributedString.length))
        }
        super.attributedText = attributedString
    }
}
-50

As a quick-dirty-smart-simple workaround:

For UILabels that don't have much lines you can instead use stackViews.

  1. For each line write a new label.
  2. Embed them into a StackView.(select both labels-->Editor-->Embed In -->StackView
  3. Adjust the Spacing of the StackView to your desired amount

Be sure to stack them vertically. This solution also works for custom fonts.

enter image description here

FWIW這是一個可怕但可行的解決方案。因此,我保留它。 - 蜂蜜18年12月22日11:44

我也看到iOS開發使用stackviews創建圖形的原則。Stackviews非常強大 - Honey 3月1日21:02

0
votes
answers
17 views
+10

Swift 3:預先加載SecondViewController的WKWebView提前點擊

0

我需要一些幫助。 我想在第一個視圖控制器中的webView完成後在第二個視圖控制器中預載我的webView。Swift 3:預先加載SecondViewController的WKWebView提前點擊

我相信我需要使用通知中心post/observe在我的第二個視圖控制器中調用我的loadWebView函數,但不知道如何。任何建議表示讚賞。


enter image description here

FirstViewController.swift

import UIKit; 
import WebKit; 

class FirstViewController: UIViewController, WKNavigationDelegate { 


    let webView:WKWebView = WKWebView(frame: CGRectMake(0, 0, UIScreen.main.bounds.width, UIScreen.main.bounds.height)) 

    func webView(_ webView: WKWebView, 
      didFinish navigation: WKNavigation!) { 
     print("loaded!") 
     webView.isHidden = false 

     } 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     let url = URL(string: "https://www.google.com") 
     webView.navigationDelegate = self 
     webView.load(URLRequest(url: url!)) 
     webView.isHidden = true 
     self.view.addSubview(webView) 

    } 

} 

SecondViewController.swift

import UIKit 
import WebKit 

class SecondViewController: UIViewController, WKNavigationDelegate { 

    let webView:WKWebView = WKWebView(frame: CGRectMake(0, 0, UIScreen.main.bounds.width, UIScreen.main.bounds.height)) 


    func webView(_ webView: WKWebView, 
      didFinish navigation: WKNavigation!) { 
     print("loaded!") 
     webView.isHidden = false 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 


    } 

    func loadWebView() { 
     let url = URL(string: "https://www.amazon.com") 
     webView.navigationDelegate = self 
     webView.load(URLRequest(url: url!)) 
     webView.isHidden = true 
     self.view.addSubview(webView) 
    } 


} 
沙发
0
0

在第一個VC:

func webView(_ webView: WKWebView, 
      didFinish navigation: WKNavigation!) { 

    //Post notification 
    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "WebViewFinishedLoading"), object: nil) 
} 

在二VC:

override func viewDidLoad() { 
     super.viewDidLoad() 

    // Add observer for your notification 
    NotificationCenter.default.addObserver(self, selector: #selector(loadWebView), name: NSNotification.Name(rawValue: "WebViewFinishedLoading"), object: nil) 
    } 

//Remove observer in deinit 
deinit {  
    NotificationCenter.default.removeObserver(self) 
} 
+0

如果secondViewController是不是在堆棧呢? –

+0

在初始化之前,您無法加載它。你到底在做什麼? –

0
votes
answers
34 views
+10

DispatchSource.makeTimerSource當按下後退按鈕時崩潰應用程序Swift

-2

我已經實現了使用DispatchSource和計時器工作的錄製音頻(錄製和播放工作正常)的計時器,但是當我按下我的導航控制器選項卡上的後退按鈕時,應用程序崩潰。我可以毫無問題地前進到下一個屏幕。 錯誤:線程1:EXC_BAD_INSTRUCTION(code = EXC_I386_INVOP,subcode = 0x0)DispatchSource.makeTimerSource當按下後退按鈕時崩潰應用程序Swift

有人請給我一個暗示,爲什麼這是?

我的看法控制器代碼:

import Foundation 
import UIKit 
import AVFoundation 

class RecordGreetingController: UIViewController { 

@IBOutlet weak var timeLabel: UILabel! 
var audioPlayer: AVAudioPlayer! 
var updateTimer = DispatchSource.makeTimerSource(flags: [], queue: DispatchQueue.main) 

override func viewDidLoad() { 
    super.viewDidLoad() 
} 

@IBAction func toggleRecordButton(_ sender: UIButton) { 
    if AudioRecorderManager.shared.recorder == nil { 
     if AudioRecorderManager.shared.recordAudio(fileName: "NewRecording") { 
      updateTimer.resume() 
     } 

     let formatter = DateComponentsFormatter() 
     formatter.zeroFormattingBehavior = .pad 
     formatter.includesApproximationPhrase = false 
     formatter.allowedUnits = [.minute, .second] 
     formatter.calendar = Calendar.current 

     updateTimer.schedule(deadline: DispatchTime.now(), repeating: DispatchTimeInterval.milliseconds(100)) 

     updateTimer.setEventHandler { [weak self] in 
      self?.timeLabel.text = formatter.string(from: AudioRecorderManager.shared.recorder!.currentTime) 
     } 
    } 
    else { 
     AudioRecorderManager.shared.finishRecording() 
     AudioRecorderManager.shared.recorder = nil 
     updateTimer.suspend() 
    } 
} 

}

我AudioRecorderManager類:

import Foundation 
import AVFoundation 

class AudioRecorderManager: NSObject, AVAudioRecorderDelegate { 


static let shared = AudioRecorderManager() 

var recordingSession: AVAudioSession! 
var recorder: AVAudioRecorder? 


func setup() { 

    recordingSession = AVAudioSession.sharedInstance() 

    do { 
     try recordingSession.setCategory(AVAudioSessionCategoryPlayAndRecord, with: .defaultToSpeaker) 
     try recordingSession.setActive(true) 

     // Request permission from user 
     recordingSession.requestRecordPermission{ (allowed) in 
      if allowed { 
       print("Mic authorised") 
      } 
      else { 
       print("Mic not authorised") 
       // TODO display alert to allow microphone access in settings to operate 
      } 
     } 
    } 
    catch { 
     print("Failed to set category", error.localizedDescription) 
    } 
} 

var meterTimer: Timer? 
var recorderApc0: Float? = 0 
var recorderPeak0: Float? = 0 

// Starts the recording session 
func recordAudio(fileName:String) -> Bool { 
    let url = getUserPath().appendingPathComponent(fileName+".m4a") 
    let audioURL = URL.init(fileURLWithPath: url.path) 
    let recordSettings: [String: Any] = [ 
     AVFormatIDKey: NSNumber(value: kAudioFormatAppleLossless), 
     AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue, 
     AVEncoderBitRateKey: 12000.0, 
     AVNumberOfChannelsKey: 1, 
     AVSampleRateKey: 44100.0 
    ] 

    do { 
     recorder = try AVAudioRecorder(url: audioURL, settings: recordSettings) 
     recorder?.delegate = self 
     recorder?.isMeteringEnabled = true 
     recorder?.prepareToRecord() 
     recorder?.record() 

     self.meterTimer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true, block: { (timer: Timer) in 
      // Update recorder meter values 
      if let recorder = self.recorder { 
       recorder.updateMeters() 
       self.recorderApc0 = recorder.averagePower(forChannel: 0) 
       self.recorderPeak0 = recorder.peakPower(forChannel: 0) 
      } 
     }) 
     print("Recording") 
     return true 
    } 
    catch { 
     print("Error recording") 
     return false 
    } 
} 

// Stop recording 
func finishRecording() { 
    self.recorder?.stop() 
    self.meterTimer?.invalidate() 
} 

// Gets path for the folder the file is being saved to 
func getUserPath() -> URL { 
    return FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] 
} 

func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool) { 
    print("Audio Manager did finish recording", flag) 
} 

func audioRecorderEncodeErrorDidOccur(_ recorder: AVAudioRecorder, error: Error?) { 
    print("Error encoding ", error?.localizedDescription ?? "") 
} 

}

+0

無關,但同時也很難效仿,它看起來像有就是你比你先恢復計時器執行的路徑設置處理程序和時間表... – Rob

+0

嗨,羅布,只是爲了澄清,我有一個updateTimer.resume()在toggleRecordButton IBAction塊的開始。你是否建議在處理程序和時間表之前恢復計時器是錯誤的? – elarcoiris

+0

是的,我建議你在啓動之前配置定時器。如果在完成配置之前啓動它,您希望它做什麼?如果你幸運的話,什麼都不會出錯,但爲什麼依賴這種無證的行爲呢?在嘗試啓動之前完成配置是最安全的。 – Rob

沙发
0
-1

這最終被解決我的問題。忽略來自上述兩個誰沒有解決這個問題的一個心懷不滿的向下投票:

var updateTimer: DispatchSourceTimer? 
@IBOutlet weak var timeLabel: UILabel! 

func startTimer() { 
    let formatter = DateComponentsFormatter() 
    formatter.zeroFormattingBehavior = .pad 
    formatter.includesApproximationPhrase = false 
    formatter.allowedUnits = [.minute, .second] 
    formatter.calendar = Calendar.current 
    let queue = DispatchQueue.main 
    updateTimer = DispatchSource.makeTimerSource(queue: queue) 
    updateTimer!.schedule(deadline: .now(), repeating: .milliseconds(100)) 
    updateTimer!.setEventHandler { [weak self] in 
     self?.timeLabel.text = formatter.string(from: AudioRecorderManager.shared.recorder!.currentTime) 
    } 
    updateTimer!.resume() 
} 

func stopTimer() { 
    updateTimer?.cancel() 
    updateTimer = nil 
} 

deinit { 
    self.stopTimer() 
} 

@IBAction func toggleRecordButton(_ sender: UIButton) { 
    if AudioRecorderManager.shared.recorder == nil { 
     if AudioRecorderManager.shared.recordAudio(fileName: "NewRecording") { 
       startTimer() 
     } 
    } 
    else { 
     stopTimer() 
     AudioRecorderManager.shared.finishRecording() 
     AudioRecorderManager.shared.recorder = nil 
    } 
} 
0
votes
answers
11 views
+10

如何將特定值放入數組數組中的一個元素

0

我想知道如何將名稱值「ok」和「123」放入第一個數組,這是[「here」]我希望其他數組爲空。如何將特定值放入數組數組中的一個元素

import Foundation 
    import CoreLocation 

    class Spot { 

     let name: String 
     let location: CLLocation 

     init (lat: CLLocationDegrees, lng: CLLocationDegrees, name: String = "") { 
      self.location = CLLocation(latitude: lat, longitude: lng) 
      self.name = name 
     } 
    }//class 

    extension Spot { 

     static var list: [Spot] { 

      return [ 

       Spot(lat: 35.124585, lng: 135.154821, name: "ok"), 
       Spot(lat: 35.05406, lng: 136.215414, name: "123") 
    ] 
     } 
    } 

    var array = [["here"],[],[],[],[],[]] 
沙发
0
1

您需要訪問的第一個數組的數組的數組中,然後將這些元素在這樣的:

array[0].append("123") 
array[0].append("ok") 

可以達到同樣的目標是這樣的:

array[0] += ["123", "ok"] 
+0

對於遲到的回覆感到抱歉。非常感謝你,我可以達到我想要的。再次感謝你!! – Daibaku

+0

不客氣,我很高興它幫助你 –