Make first letter Bold - ios swift

喜欢而已 提交于 2019-12-12 02:59:38

问题


I am creating an application for Dutch and English language. So I add my string values to Localizable.strings file. Now I want to change only one items first letter should be capitalized. For example, my string like this

A. I am in.

B. I am out.

I want to capitalize A. and B. and C. etc. I want to do it programmatically.

How can I do this in ios using swift as a language.


回答1:


Try to add attribute to string i.e.

yourString.addAttribute(NSFontAttributeName, value: UIFont( name: "HelveticaNeue-Bold", size: 18.0)!, range: NSRange( location:2, length:4))

here you can decide location for word in string.Hope it will work :) Also please refer following link :- http://makeapppie.com/2014/10/20/swift-swift-using-attributed-strings-in-swift/




回答2:


Try like this:

extension UILabel {
    func setFirstLetterCapitalizedBold() {
        if text?.characters.count > 0 {
            let attText = NSMutableAttributedString(string: String(text!.characters.first!).uppercaseString + String(text!.characters.dropFirst()))
            attText.setAttributes([NSFontAttributeName:UIFont.boldSystemFontOfSize(font.pointSize)], range: NSRange(location: 0, length: 1))
            attributedText = attText
        }
    }
}

let myLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 50))
myLabel.font = UIFont(name: "Arial Black", size: 20)

myLabel.text = "a. I am "
myLabel.setFirstLetterCapitalizedBold()

myLabel


来源:https://stackoverflow.com/questions/35331812/make-first-letter-bold-ios-swift

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!