最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]swift中使用带属性的字符串

iOS crifan 1683浏览 0评论

之前弄过attributed string:

        let attributedCenterText:NSMutableAttributedString = NSMutableAttributedString(string: centerText)

        attributedCenterText.addAttributes([NSParagraphStyleAttributeName : paraStyle], range: NSMakeRange(0, centerText.characters.count))

但是不熟悉。

现在想要把:

的实现,从:

        let todoHeaderHeight:CGFloat = 20

//        let todoLabelHeight:CGFloat = 16

//        let todoLabelHeight:CGFloat = todoHeaderHeight

        let todoHeaderView = UIView()

        todoHeaderView.frame = CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, todoHeaderHeight)

        gLog.verbose("todoHeaderView.frame=\(todoHeaderView.frame)") //(0.0, 0.0, 375.0, 20.0)

        todoHeaderView.backgroundColor = UIColor.clearColor()

        let todoLabel:UILabel = UILabel()

        todoLabel.text = "今日代办"

        todoLabel.textAlignment = .Left

//        todoLabel.contentMode = .Center

        todoLabel.font = UIFont.systemFontOfSize(14)

        todoHeaderView.addSubview(todoLabel)

        constrain(todoLabel) {todoLabel in

            todoLabel.top == todoLabel.superview!.top

            todoLabel.left == todoLabel.superview!.left + 8

//            todoLabel.right <= todoLabel.superview!.right

            todoLabel.bottom == todoLabel.superview!.bottom

//            todoLabel.height == todoLabelHeight

        }

        let curTodoNumber:Int = 6

        let todoNumLabel:UILabel = UILabel()

        todoNumLabel.text = "(\(curTodoNumber))"

        todoNumLabel.textAlignment = .Left

        todoNumLabel.textColor = UIColor.redColor()

//        todoNumLabel.contentMode = .Center

        todoNumLabel.font = todoLabel.font

        todoHeaderView.addSubview(todoNumLabel)

        constrain(todoNumLabel, todoLabel) {todoNumLabel, todoLabel in

            todoNumLabel.top == todoNumLabel.superview!.top

            todoNumLabel.left == todoLabel.right + 4

//            todoNumLabel.right <= todoNumLabel.superview!.right

            todoNumLabel.bottom == todoNumLabel.superview!.bottom

//            todoNumLabel.height == todoLabelHeight

        }

        self.todoTableView.tableHeaderView = todoHeaderView

变成带属性的字符串,只用一个,就可以了,不用两个普通的字符串了。

swift attributedstring

Attributed Strings Tutorial in iOS8 with Swift – iOScreator

fonts – How do I make an attributed string using Swift? – Stack Overflow

How to make tappable links in NSAttributedString – Swift 2 example code

iOS8 Attributed strings教程-Swift – swift迷

-》

UIFont – NSAttributedString Class Reference

改为:

        let redNumAttribute = [NSForegroundColorAttributeName : UIColor.redColor()]

        let fontAttribute = [NSFontAttributeName: UIFont.systemFontOfSize(15)]

        let nonRedStr:String = "今日代办 "

        let attributedStr:NSMutableAttributedString = NSMutableAttributedString(string: nonRedStr + "(\(curTodoNumber))")

        attributedStr.addAttributes(fontAttribute, range: NSRange(location: 0, length: attributedStr.length))

        attributedStr.addAttributes(redNumAttribute, range: NSRange(location: nonRedStr.characters.count, length: attributedStr.length – nonRedStr.characters.count))

        let todoHeaderLabel = UILabel()

        todoHeaderLabel.textAlignment = .Left

        todoHeaderLabel.attributedText = attributedStr

        todoHeaderLabel.frame = CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, todoHeaderHeight)

        gLog.verbose("todoHeaderView.frame=\(todoHeaderLabel.frame)") //(0.0, 0.0, 375.0, 20.0)

        todoHeaderLabel.backgroundColor = UIColor.clearColor()

        self.todoTableView.tableHeaderView = todoHeaderLabel

效果:

但是想要左边对齐的话,直接改为:

        todoHeaderLabel.frame = CGRectMake(8, 0, UIScreen.mainScreen().bounds.width, todoHeaderHeight)

是没有效果的。

-》还是需要:

把label作为subview加到header view中才可以。

-》也不是足够简洁。

-》对于后期更新数字到话,也有点麻烦。

-》所以此处还是用之前的办法,用两个label去实现,易于修改后面的label的数字。

[总结]

不可修改的:NSAttributedString

可变可修改的:NSMutableAttributedString

详见:

fonts – How do I make an attributed string using Swift? – Stack Overflow

关于内置哪些属性,有:

let NSAttachmentAttributeName: String

let NSBackgroundColorAttributeName: String

let NSBaselineOffsetAttributeName: String

let NSExpansionAttributeName: String

let NSFontAttributeName: String

let NSForegroundColorAttributeName: String

let NSKernAttributeName: String

let NSLigatureAttributeName: String

let NSLinkAttributeName: String

let NSObliquenessAttributeName: String

let NSParagraphStyleAttributeName: String

let NSShadowAttributeName: String

let NSStrikethroughColorAttributeName: String

let NSStrikethroughStyleAttributeName: String

let NSStrokeColorAttributeName: String

let NSStrokeWidthAttributeName: String

let NSTextEffectAttributeName: String

let NSUnderlineColorAttributeName: String

let NSUnderlineStyleAttributeName: String

let NSVerticalGlyphFormAttributeName: String

let NSWritingDirectionAttributeName: String

详见:

UIFont – NSAttributedString Class Reference

[后记]

后来看到:

NSMutableAttributedString Class Reference

有:replaceCharactersInRange

-》说明修改内容,好像也是方便的。

去试试

        let redNumAttribute = [NSForegroundColorAttributeName : UIColor.redColor()]

        let fontAttribute = [NSFontAttributeName: UIFont.systemFontOfSize(15)]

        let nonRedStr:String = "今日代办 "

        let attributedStr:NSMutableAttributedString = NSMutableAttributedString(string: nonRedStr + "(\(curTodoNumber))")

        attributedStr.addAttributes(fontAttribute, range: NSRange(location: 0, length: attributedStr.length))

        let redStrRange = NSRange(location: nonRedStr.characters.count, length: attributedStr.length – nonRedStr.characters.count)

        attributedStr.addAttributes(redNumAttribute, range: redStrRange)

        let todoHeaderLabel = UILabel()

        todoHeaderLabel.textAlignment = .Left

        todoHeaderLabel.attributedText = attributedStr

        todoHeaderLabel.frame = CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, todoHeaderHeight)

        gLog.verbose("todoHeaderView.frame=\(todoHeaderLabel.frame)") //(0.0, 0.0, 375.0, 20.0)

        todoHeaderLabel.backgroundColor = UIColor.clearColor()

        attributedStr.replaceCharactersInRange(redStrRange, withString: "(\(23))")

        todoHeaderLabel.attributedText = attributedStr

        self.todoTableView.tableHeaderView = todoHeaderLabel

效果:

-》也还是很方便的。

转载请注明:在路上 » [已解决]swift中使用带属性的字符串

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.384 seconds, using 22.06MB memory