【问题标题】:Trouble with formatting of OHAttributedLabel when parsing markup解析标记时出现 OHAttributedLabel 格式问题
【发布时间】:2013-11-08 00:54:14
【问题描述】:

在属性列表中,我有一个这样的字符串:

<string>A |picture| is worth *1000\* words.</string>

显然“图片”应该是斜体,“1000”应该是粗体。我正在尝试使用OHAttributedLabel 显示文本:

NSString *theString = [pListData objectForKey:@"theString"];

NSMutableAttributedString* attrStr = [OHASBasicMarkupParser attributedStringByProcessingMarkupInString:theString];

self.myLabel.attributedText = attrStr; //self.myLabel is a UILabel

这是我得到的:

为什么属性字以较小的字体显示,我该如何解决这个问题?

【问题讨论】:

  • 如果你已经解决了,请回答你的问题,否则我可以开始赏金

标签: ios uilabel nsattributedstring


【解决方案1】:

我以前处理过类似的事情。所以,这会对你有所帮助。我也为你测试过,效果很好:)

NSString *string = @"A |picture| is worth *1000* words.";

// Create an NSMutableAttributedString in order to be able to use the NSAttributedString+Attributes.h category
NSMutableAttributedString *attributedString = [NSMutableAttributedString attributedStringWithString:string];

// If using custom fonts, import all styles in the app and update the plist file
[attributedString setFont:[UIFont fontWithName:@"Helvetica" size:20.0]];

// Parse the markup and set the attributedText of the UILabel instance (no need to use the OHAttributedLabel subclass)
attributedString = [OHASBasicMarkupParser attributedStringByProcessingMarkupInAttributedString:attributedString];
label.attributedText = attributedString;

编辑:在调用-attributedStringByProcessingMarkupInAttributedString 之后使用-setFont: 时会出现问题,因为这会重置解析期间完成的所有工作。

【讨论】:

  • 嗯,这也适用于我,但你能详细说明一下如何以及为什么?
  • 恐怕我不明白你的问题。什么意思?
  • 没关系,我想我知道问题所在 - 我会编辑你的答案并给你赏金。
猜你喜欢
  • 2021-10-11
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-28
  • 1970-01-01
相关资源
最近更新 更多