gpt4 book ai didi

ios - NSAttributedString 添加文本对齐

转载 作者:IT老高 更新时间:2023-10-28 12:20:58 24 4
gpt4 key购买 nike

如何将文本对齐属性添加到 NSAttributedString 以使文本居中?

编辑:我做错什么了吗?它似乎没有改变对齐方式。

CTParagraphStyleSetting setting;
setting.spec = kCTParagraphStyleSpecifierAlignment;
setting.valueSize = kCTCenterTextAlignment;

CTParagraphStyleSetting settings[1] = {
{kCTParagraphStyleSpecifierAlignment, sizeof(CGFloat), &setting},
};

CTParagraphStyleRef paragraph = CTParagraphStyleCreate(settings, sizeof(setting));

NSMutableAttributedString *mutableAttributed = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedString];
[mutableAttributed addAttributes:[NSDictionary dictionaryWithObjectsAndKeys:(NSObject*)paragraph ,(NSString*) kCTParagraphStyleAttributeName, nil] range:_selectedRange];

最佳答案

 NSMutableParagraphStyle *paragraphStyle = NSMutableParagraphStyle.new;
paragraphStyle.alignment = NSTextAlignmentCenter;

NSAttributedString *attributedString =
[NSAttributedString.alloc initWithString:@"someText"
attributes:
@{NSParagraphStyleAttributeName:paragraphStyle}];

swift 4.2

let paragraphStyle: NSMutableParagraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.center

let attributedString = NSAttributedString(string: "someText", attributes: [NSAttributedString.Key.paragraphStyle : paragraphStyle])

关于ios - NSAttributedString 添加文本对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6801856/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com