【问题标题】:How to add multiple line text in UIActionSheet如何在 UIActionSheet 中添加多行文本
【发布时间】:2019-09-05 13:35:11
【问题描述】:

如何在 UIActionSheet swift 中添加带有自定义字体的多行文本。我已经尝试过\n。但这不起作用。这是否可能。

这是我的代码。

    alert.addAction(UIAlertAction(title: "line 1.\n %C line 2,\n %C line", style: .default , handler:{ (UIAlertAction)in
        print("User click Approve button")
    }))

    alert.addAction(UIAlertAction(title: "Edit", style: .default , handler:{ (UIAlertAction)in
        print("User click Edit button")
    }))

    alert.addAction(UIAlertAction(title: "Delete", style: .destructive , handler:{ (UIAlertAction)in
        print("User click Delete button")
    }))

    alert.addAction(UIAlertAction(title: "Dismiss", style: .cancel, handler:{ (UIAlertAction)in
        print("User click Dismiss button")
    }))

    self.present(alert, animated: true, completion: {
        print("completion block")
    })

【问题讨论】:

    标签: swift xcode uialertview uiactionsheet nsobject


    【解决方案1】:

    试试下面的代码。

    let optionMenu = UIAlertController(title: "Choose Class", message: "", preferredStyle: .actionSheet)
    let course1 = UIAlertAction(title: "Computer Science(1st year) \n Digital Electronics", style: .default)
    let course2 = UIAlertAction(title: "Computer Science(2nd year) \n Digital Electronics", style: .default)
    let cancel = UIAlertAction(title: "Cancel", style: .cancel)
    optionMenu.addAction(course1)
    optionMenu.addAction(course2)
    optionMenu.addAction(cancel)
    self.present(optionMenu, animated: true, completion: nil)
    
    // Setting up the number of lines and doing a word wrapping        
    UILabel.appearance(whenContainedInInstancesOf:[UIAlertController.self]).numberOfLines = 2
    UILabel.appearance(whenContainedInInstancesOf:[UIAlertController.self]).lineBreakMode = .byWordWrapping
    

    输出

    【讨论】:

    • 感谢 chirag 的宝贵回复。这是多行的工作,你能建议我如何添加多种字体..
    • 好的,我会补充的。
    【解决方案2】:

    我在 UIAlertAction 中显示了 2 个文本值。 要使用多种字体、颜色和其他属性,我们必须使用attributedText 属性。 我使用了以下代码。

    UILabel.appearance(whenContainedInInstancesOf: [UIAlertController.self]).numberOfLines = 0
    
    let actionSheetController = UIAlertController(title: nil, message: "Call", preferredStyle: .actionSheet)
    actionSheetController.view.tintColor = UIColor.red
    
    for key in phoneNumbers.keys {
        let phoneNumber = phoneNumbers[key] ?? ""
        let actionTitle = "\(key)\n\(phoneNumber)"
        let phoneNumberAction = UIAlertAction(title: actionTitle, style: .default) { action -> Void in
            self.makeCall(phoneNumber)
        }
        actionSheetController.addAction(phoneNumberAction)
    
        let attributedText = NSMutableAttributedString(string: actionTitle)
    
        let range = NSRange(location: key.count, length: attributedText.length - key.count)
    
        attributedText.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.gray, range: range)
    
        guard let label = (phoneNumberAction.value(forKey: "__representer")as? NSObject)?.value(forKey: "label") as? UILabel else { return }
        label.attributedText = attributedText
    }
    
    let cancelAction = UIAlertAction(title: LOCALSTR("Cancel"), style: .cancel) { action -> Void in
    
    }
    actionSheetController.addAction(cancelAction)
    
    present(actionSheetController, animated: true, completion: nil)
    

    【讨论】:

    • 顺便说一句,我看到另一个问题。 stackoverflow.com/questions/61216417/…
    • 无法访问标签属性
    • @KedarSukerkar 你的意思是guard let label = (phoneNumberAction.value(forKey: "__representer")as? NSObject)?.value(forKey: "label") as? UILabel else { return } 不起作用或label.attributedText = attributedText
    • 是的.....对不起,这是我这边的错误......我在显示警报之前正在访问标签。这就是为什么变得为零的原因......另外,我无法更改字体属性..任何解决方案。
    • 是的,我添加了类似于this..attributedText.addAttribute(NSAttributedString.Key.font, value: myFont, range: range)....但是没有效果
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-10
    • 1970-01-01
    相关资源
    最近更新 更多