我继承NSTextStorage做一些链接高亮和我多读我可以对 话题.一切正常,直到我输入 emoji character.
private let ims = NSMutableAttributedString()
override var string: String {
return ims.string
}
override func attributesAtIndex(location: Int, effectiveRange range: NSRangePointer) -> [String : AnyObject] {
return ims.attributesAtIndex(location, effectiveRange: range)
}
override func replaceCharactersInRange(range: NSRange, withString str: String) {
ims.replaceCharactersInRange(range, withString: str)
self.edited(.EditedCharacters, range: range, changeInLength:(str as NSString).length - range.length)
}
override func setAttributes(attrs: [String : AnyObject]?, range: NSRange) {
ims.setAttributes(attrs, range: range)
self.edited(.EditedAttributes, range: range, changeInLength: 0)
}
Run Code Online (Sandbox Code Playgroud)
Nothing complicated. …
objective-c ios nstextstorage nsmutableattributedstring swift
我正在创建一个应用程序,我必须实现这样的功能:
1)写入textview
2)从textview中选择文本
3)允许用户对所选文本应用粗体,斜体和下划线功能.
我已经开始使用NSMutableAttributedString实现它.它适用于粗体和斜体,但仅使用选定的文本替换textview文本.
-(void) textViewDidChangeSelection:(UITextView *)textView
{
rangeTxt = textView.selectedRange;
selectedTxt = [textView textInRange:textView.selectedTextRange];
NSLog(@"selectedText: %@", selectedTxt);
}
-(IBAction)btnBold:(id)sender
{
UIFont *boldFont = [UIFont boldSystemFontOfSize:self.txtNote.font.pointSize];
NSDictionary *boldAttr = [NSDictionary dictionaryWithObject:boldFont forKey:NSFontAttributeName];
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc]initWithString:selectedTxt attributes:boldAttr];
txtNote.attributedText = attributedText;
}
Run Code Online (Sandbox Code Playgroud)
有人可以帮我实现这个功能吗?
提前致谢.
刚开始学习Swift并创建了一个小macOS应用程序,我想在其中使用a NSScrollView来显示属性String.我试过了:
@IBOutlet var ScrollViewOutlet : NSScrollView
var attributedString = NSMutableAttributedString(string: myText)
ScrollViewOutlet.insertText(attributedString)
Run Code Online (Sandbox Code Playgroud)
但这似乎不起作用.我发现有点令人困惑,因为做一个相同的NSTextView工作就像一个魅力.
我在这里错过了什么?
我有一个不可变的属性字符串,没有NSStrikethroughStyleAttributeName属性,如下所示:
NSMutableAttributedString *str1 = [[NSMutableAttributedString alloc] initWithString:@"aaaa" attributes:@{NSStrikethroughStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle]}];
Run Code Online (Sandbox Code Playgroud)
和另一个带有NSStrikethroughStyleAttributeName属性的可变属性字符串,如下所示:
NSMutableAttributedString *str2 = [[NSMutableAttributedString alloc] initWithString:@"bbbb"];
Run Code Online (Sandbox Code Playgroud)
以及包含上面两个字符串的整个字符串:
NSMutableAttributedString *labelString = [[NSMutableAttributedString alloc] init];
[labelString appendAttributedString:str1];
[labelString appendAttributedString:str2];
Run Code Online (Sandbox Code Playgroud)
当我将整个字符串附加到UILabel时:
_label.attributedText = labelString;
Run Code Online (Sandbox Code Playgroud)
它在iOS7和iOS8中都表现良好,如下所示:
AAAABBBB
但是当我交换他们的职位时:
[labelString appendAttributedString:str2];
[labelString appendAttributedString:str1];
Run Code Online (Sandbox Code Playgroud)
它在iOS7中正确显示,但在iOS8中没有正确显示
ios7:bbbbAAAA ios8:bbbbaaaa
似乎在iOS8中,如果属性字符串中的第一个字符不是strikethroughed,则UILabel不会正确渲染删除线.我认为这是iOS8中的一个错误,是否有人遇到过与我相同的问题?
nsattributedstring strikethrough ios nsmutableattributedstring ios8
我正在从Localizable.strings中读取字符串,其中包含类似于Android应用程序的strings.xml中的内容.
"testShort" = "A <b>short</b>\ntest with another<b>bold text</b>";
Run Code Online (Sandbox Code Playgroud)
粗体和换行是我文本中唯一的两个格式属性.我试图开发这样的方法几天没有成功:
func ConvertText(inputText: String) -> NSAttributedString {
// here comes the conversion to a representation with Helvetica 14pt and Helvetica-Bold 14pt including line feeds.
}
Run Code Online (Sandbox Code Playgroud)
我的最终目标是在UITextView的attributedText中显示文本.
在不知道Objective-C的情况下成为Swift和iOS的新手我发现很难做String操作,因为它们非常不同且复杂,所有示例都在Objective-C中.更令人困难的是,大多数API方法在Swift中不可用或与Objective-C不同......
这是我到目前为止尝试的方法体,这里有很多其他帖子:
var test = inputText.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)!
attrStr = NSAttributedString(
data: test,
options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil,
error: nil)!
return attrStr
Run Code Online (Sandbox Code Playgroud)
这里的主要问题\n是没有转换,字体非常小而且不同.
接下来,我尝试手动加粗文本的一部分.它看起来像那样:
var newText = NSMutableAttributedString(string: inputText)
newText.addAttribute(NSFontAttributeName, value: UIFont(name: "Helvetica-Bold", size: 14.0)!, range: NSRange(location:2,length:4))
Run Code Online (Sandbox Code Playgroud)
现在我尝试在文本中搜索属性,删除它们并使用类似的addAttribute
// Start of bold text
var …Run Code Online (Sandbox Code Playgroud) xcode nsattributedstring ios nsmutableattributedstring swift
我想将所有属性从一个复制NSMutableAttributedString到一个新属性.我试过的代码是这样的:
[attrStr enumerateAttribute:NSFontAttributeName inRange:NSMakeRange(0, attrStr.length) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) {
if (value) {
// UIFont *oldFont = (UIFont *)value;
UIFont *newFont = [_label.attributedText
[attrStr removeAttribute:NSFontAttributeName range:range];
[attrStr addAttribute:NSFontAttributeName value:newFont range:range];
//found = YES;
}
}];
Run Code Online (Sandbox Code Playgroud)
代码显然是不完整的,看起来它试图只为字体做.我想遍历每个属性并将其添加到一个新NSMutableAttributedString变量中.更新:我的问题是如何将一个属性NSMutableAttributedString应用于另一个属性NSMutableAttributedString?我们可以使用这种方法吗?somehow:attribute:atIndex:effectiveRange
我使用下面的代码来改变foreground color和font使用属性字符串Swift。但是字体完美地改变没有任何问题,但foreground color没有改变
var checklistText = NSMutableAttributedString()
checklistText = NSMutableAttributedString(string: "\(lblChecklistName.text!),\(checklistName)")
checklistText.addAttribute(NSFontAttributeName,
value: UIFont(
name: "Helvetica",
size: 11.0)!,
range: NSRange(location: lblChecklistName.text!.length(), length: checklistName.length()))
checklistText.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSRange(location: lblChecklistName.text!.length(), length: checklistName.length()+1))
lblChecklistName.attributedText = checklistText
Run Code Online (Sandbox Code Playgroud) 我已经看了大约10个关于SO的问题,而且我仍然很短.
我有一个多行UILabel(在Interface Builder中创建,numberOfLines设置为0),它通常像这样呈现:

我想强调"服务条款"和"隐私政策",所以我添加了以下代码:
NSString *text = self.agreement.text;
NSMutableAttributedString *aString = [[NSMutableAttributedString alloc] initWithString:text];
NSRange privacyRange = [text rangeOfString:@"privacy policy" options:NSCaseInsensitiveSearch];
[aString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:privacyRange];
NSRange tosRange = [text rangeOfString:@"terms of service" options:NSCaseInsensitiveSearch];
[aString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:tosRange];
self.agreement.attributedText = aString;
Run Code Online (Sandbox Code Playgroud)
但结果如下:

我需要做什么才能显示这两行,并用适当的范围加下划线?
此外,我不想使用像OHAttributedLabel或TTTAttributedLabel这样的第三方库,因为这是我的应用中唯一需要为一段文字加下划线的地方.
我试过的
sizeToFit设置属性文本后调用numberOfLines和lineBreakMode代码Sascha让我上传了两个截图,背景颜色设置为清除之外的其他颜色.奇怪的是,一切都按预期显示!不知道该怎么做或这告诉我们什么.

objective-c nsattributedstring uilabel ios nsmutableattributedstring
我有一个表视图,其中单元格有一个带有一些属性文本的标签.正确设置了文本cellForRowAtIndexPath.正确设置了文本的颜色,但在单元格出列之前,不会显示粗体属性.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
Model *model = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.tag = indexPath.row;
[cell updateContentWithModel:model atIndexPath:indexPath];
return cell;
}
- (void)updateContentWithModel:(Model *)model atIndexPath:(NSIndexPath *)indexPath
{
self.model = model;
[self setTextFromModel];
[self setImageFromModelAtIndexPath:indexPath];
}
- (void) setTextFromModel
{
self.title.text = self.model.header;
self.status.attributedText = [self boldString:self.model.status fontSize:self.status.font.pointSize color:[UIColor redColor]];
}
+(NSMutableAttributedString *)boldString:(NSString *)stringToBold fontSize:(CGFloat)fontSize color:(UIColor *)color
{
NSMutableAttributedString *boldString = [[NSMutableAttributedString alloc] initWithString:stringToBold];
[boldString setAttributes:@{NSForegroundColorAttributeName: color,
NSFontAttributeName:[UIFont boldSystemFontOfSize:fontSize]} range:NSMakeRange(0, boldString.length)];
return boldString;
} …Run Code Online (Sandbox Code Playgroud) let paragraph = NSMutableParagraphStyle()
paragraph.alignment = NSTextAlignment.right
paragraph.tailIndent = -3
let shadow = NSShadow()
shadow.shadowBlurRadius = 5
shadow.shadowColor = UIColor.gray
shadow.shadowOffset = CGSize(width: 2, height: -2)
let text = NSMutableAttributedString(string: "MY LABEL", attributes:
[NSShadowAttributeName : shadow,
NSStrokeColorAttributeName : UIColor.black,
NSStrokeWidthAttributeName : -3,
NSForegroundColorAttributeName: UIColor.white,
NSFontAttributeName : UIFont(name: "MyFont", size: 24) as Any,
NSParagraphStyleAttributeName : paragraph])
let node = SKLabelNode()
addChild(node)
node.attributedText = attributedString
Run Code Online (Sandbox Code Playgroud)
它有效,但没有阴影。我可以使用相同的NSMutableAttributedString上著名的ASAttributedLabel和它的作品不错,有一个影子,但我想使用SKLabelNode,以实现更好的性能IOS11。ASAttributedLabel可能会滞后于动态场景:(
nsattributedstring nsmutableattributedstring sprite-kit sklabelnode swift
ios ×8
swift ×5
objective-c ×2
xcode ×2
ios8 ×1
nsscrollview ×1
sklabelnode ×1
sprite-kit ×1
uilabel ×1
uitableview ×1
uitextview ×1