ios - 属性文本居中对齐

标签 ios xcode label nsattributedstring centering

我已经尝试了所有方法,但似乎无法将此文本居中。谁能告诉我错误在哪里。

NSMutableParagraphStyle *paragraphStyle = NSMutableParagraphStyle.new;
paragraphStyle.alignment = NSTextAlignmentCenter;
label.attributedText = [[NSAttributedString alloc] initWithString:cell.EventTitle.text attributes:@{NSForegroundColorAttributeName : [UIColor whiteColor],NSParagraphStyleAttributeName:paragraphStyle,NSBaselineOffsetAttributeName : @0,NSFontAttributeName : [UIFont fontWithName:@"BrandonGrotesque-Black" size:34]}];

最佳答案

在 Swift 5 中

let paragraph = NSMutableParagraphStyle()
paragraph.alignment = .center
textView.attributedText = NSAttributedString(string: "String",
                                                     attributes: [.paragraphStyle: paragraph])

在 Swift-4 中

let paragraph = NSMutableParagraphStyle()
paragraph.alignment = .center

let attributes: [NSAttributedString.Key : Any] = [NSAttributedString.Key.paragraphStyle: paragraph]
let attrString = NSAttributedString(string:"string", attributes: attributes)
textView.attributedText =  attrString

在 Swift-3 中

let paragraph = NSMutableParagraphStyle()
paragraph.alignment = .center

let attributes: [String : Any] = [NSParagraphStyleAttributeName: paragraph]
let attrString = NSAttributedString(string:"string", attributes: attributes)
textView.attributedText =  attrString

关于ios - 属性文本居中对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29045750/

相关文章:

c# - 显示 'saved' 标签 2 秒然后在 c# asp.net 中隐藏

ios - 为什么 `HttpClient` 返回 `NSURLSessionDataTask` 但调用的 `API method` 除外 ` { JSONResult, error in`

ios - EXC_BAD_ACCESS 在 Swift 中访问自定义 MKAnnotation 的属性时

ios - XCode:更改了其他链接器标志,现在有 clang 错误

c++ - 另一个 "ld: symbol(s) not found for architecture x86_64"问题与 boost,这次是 mgiza

c# - 如何从列表框项目值设置标签文本? C#

opacity - 如何使用 CAKeyFrameAnimation 中的关键时间来动画图层的不透明度?

ios - 不常见的png文件iOS显示

ios - 在 ipod 上测试我的应用程序时收到错误消息

ios - 如何在选项卡栏 Controller 中从一个 View Controller 切换到另一个 View Controller 并保留选项卡栏?