首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CATextLayer正在忽略NSMutableParagraphStyle

CATextLayer正在忽略NSMutableParagraphStyle
EN

Stack Overflow用户
提问于 2016-03-06 12:53:59
回答 1查看 363关注 0票数 1

我想使用NSMutableAttributedStringCATextLayer上绘制包含文本的文本。我尝试通过[attrString setAttributes:attributes range:range];将属性设置为NSMutableAttributedString的字体和字体颜色,但NSMutableParagraphStyle中的所有属性都被忽略。

代码语言:javascript
复制
let str = "I want to use NSMutableAttributedString to draw the text on the CATextLayer with the text. I try set attributes to the like font and fontcolor of the NSMutableAttributedString through the [attrString setAttributes:attributes range:range]; but all attributes in the NSMutableParagraphStyle are ignored."
    let att = NSMutableAttributedString(string: str)
    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.lineSpacing = 12
    att.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSMakeRange(0, str.characters.count))
let contentLayer = initTextLayer("", andFontSize: 11)
    contentLayer.string = att
    contentLayer.frame = CGRect(x: 30 * AUTOSIZESCALEX, y: CGRectGetMaxY(titleLayer.frame) + 15 * AUTOSIZESCALEY, width: SCREEN_WIDTH - 60 * AUTOSIZESCALEX, height: 200 * AUTOSIZESCALEY)
    self.contentView.layer.addSublayer(contentLayer)

func initTextLayer(title: String, andFontSize size: CGFloat) -> CATextLayer{
    let textLayer = CATextLayer()
    textLayer.foregroundColor = UIColor(hexString: "333333").CGColor
    textLayer.string = title
    textLayer.alignmentMode = kCAAlignmentLeft
    textLayer.contentsScale = UIScreen.mainScreen().scale
    textLayer.shouldRasterize = false
    textLayer.wrapped = true
    textLayer.fontSize = size  * AUTOSIZESCALEY
    return textLayer
}

我是不是做错了什么?

EN

回答 1

Stack Overflow用户

发布于 2016-07-06 05:37:05

我遇到了类似的问题,并使用了以下代码:

代码语言:javascript
复制
    // Set text alignment. Apparently CATextLayer doesn't use value in NSAttributedString.
    if let style = text.attribute(NSParagraphStyleAttributeName, atIndex: 0, effectiveRange: nil) as? NSParagraphStyle {
        switch style.alignment {
        case .Center:
            textLayer.alignmentMode = kCAAlignmentCenter
        case .Left:
            textLayer.alignmentMode = kCAAlignmentLeft
        case .Right:
            textLayer.alignmentMode = kCAAlignmentRight
        default:
            break
        }
    }
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35823281

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档