美文网首页iOS开发资料收集区iOS开发iOS Developer
UILabel:行间距、不同字体大小、颜色设置

UILabel:行间距、不同字体大小、颜色设置

作者: 青禾ws | 来源:发表于2016-11-21 15:19 被阅读264次

    //个人信息

    let infor:String = "你好,我叫 " + masterName + "\n" + "现任(曾任)" + masterPosition + "\n" + "是 " + masterType + "\n" + "于  " + enterDate + "  入驻"

    var mutableString = NSMutableAttributedString()

    mutableString = NSMutableAttributedString(string: infor, attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: 14)])

    //行间距

    let lineSpace = NSMutableParagraphStyle.init()

    lineSpace.lineSpacing = 5

    mutableString.addAttribute(NSParagraphStyleAttributeName, value: lineSpace, range: NSRange(location:0,length:infor.characters.count))

    //颜色

    mutableString.addAttribute(NSForegroundColorAttributeName, value:WsTool.textGray(), range:NSRange(location:6,length:masterName.characters.count))

    mutableString.addAttribute(NSForegroundColorAttributeName, value:WsTool.textGray(), range:NSRange(location:13 + masterName.characters.count,length:masterPosition.characters.count))

    mutableString.addAttribute(NSForegroundColorAttributeName, value:WsTool.textGray(), range:NSRange(location:19 + masterName.characters.count + masterPosition.characters.count,length:masterType.characters.count))

    //字体大小

    mutableString.addAttribute(NSFontAttributeName, value:UIFont.systemFont(ofSize: 17), range:NSRange(location:6,length:masterName.characters.count))

    mutableString.addAttribute(NSFontAttributeName, value:UIFont.systemFont(ofSize: 17), range:NSRange(location:13 + masterName.characters.count,length:masterPosition.characters.count))

    mutableString.addAttribute(NSFontAttributeName, value:UIFont.systemFont(ofSize: 17), range:NSRange(location:19 + masterName.characters.count + masterPosition.characters.count,length:masterType.characters.count))

    //label

    let inforLabel = UILabel.init()

    //多行文本

    inforLabel.lineBreakMode = NSLineBreakMode.byWordWrapping

    inforLabel.numberOfLines = 0

    //

    inforLabel.attributedText = mutableString

    相关文章

      网友评论

        本文标题:UILabel:行间距、不同字体大小、颜色设置

        本文链接:https://www.haomeiwen.com/subject/smsupttx.html