标签: nsattributedstring

如何在 iOS 15 中使用 AttributedString 描边文本?

iOS 15 包含对AttributedString的支持

例子

struct PlaygroundView: View {
    func attributedString(string: String) -> AttributedString {
        var text = AttributedString(string)
        text.font = .largeTitle.bold()
        text.foregroundColor = .blue
        return text
    }
    
    var body: some View {
        Text(attributedString(string: "Hello"))
    }
}
Run Code Online (Sandbox Code Playgroud)

归属文本

文档提到属性范围

AttributedString API 定义了常见用途的键,例如文本样式、语义标记可格式化类型(例如日期和数字)以及超链接。您可以在 AttributeScopes 枚举中找到这些属性,其中包含 Foundation、SwiftUI、UIKit 和 AppKit 的属性。

这是描边颜色属性的描边颜色,但它似乎什么也没做。

struct PlaygroundView: View {
    func attributedString(string: String) -> AttributedString {
        var text = AttributedString(string)
        text.font = .largeTitle.bold()
        text.foregroundColor = .blue
        text.strokeWidth = 5
        text.strokeColor = .red
        return text
    } …
Run Code Online (Sandbox Code Playgroud)

uikit nsattributedstring ios swiftui

4
推荐指数
1
解决办法
4951
查看次数

如何将图像附件添加到 AttributedString 中?

我正在努力替换NSAttributedString,AttributedString但未能成功使附件正常工作。尽管我应用了附件,但图像并未出现在字符串中。

let textAttachment = NSTextAttachment(image: UIImage(systemName: "exclamationmark.triangle.fill")!)
textAttachment.accessibilityLabel = "Warning"

// Original code
label.attributedText = NSAttributedString(attachment: textAttachment)

// New code
var attributedString = AttributedString()
attributedString.attachment = textAttachment
label.attributedText = NSAttributedString(attributedString)
Run Code Online (Sandbox Code Playgroud)

nsattributedstring ios swift ios15 attributedstring

4
推荐指数
1
解决办法
1978
查看次数

可以为NSTextField设置对齐或字体,但不能同时设置两者

我有一个面板笔尖,其中一个文本字段的出口,在笔尖中设置为居中对齐.当我显示面板时,我希望这个文本字段是粗体.由于NSTextField是NSControl的子类,因此它可以使用setAttributedStringValue方法并获取属性字符串.所以我加入了这样的粗体字体:

NSFont *fontBolded = [NSFont fontWithName:@"Baskerville Bold" size:12.0f];
NSDictionary *dictBoldAttr = [NSDictionary dictionaryWithObject:fontBolded forKey:NSFontAttributeName];   
NSString *sHelloUser = NSLocalizedString(@"Hello User", @"Hello User");
NSAttributedString *attrsHelloUser = [[NSAttributedString alloc] initWithString: sHelloUser attributes:dictBoldAttr];
[self.fooController.tfPanelCenteredField setAttributedStringValue:attrsHelloUser];  
[attrsHelloUser release];
Run Code Online (Sandbox Code Playgroud)

粗体显示OK,但现在该字段左对齐.

我尝试添加setAlignment,但它没有效果:

[self.fooController.tfPanelCenteredField setAlignment:NSCenterTextAlignment];
Run Code Online (Sandbox Code Playgroud)

所以我尝试将一个居中的parapraph样式添加到属性字符串的属性中:

NSFont *fontBolded = [NSFont fontWithName:@"Baskerville Bold" size:12.0f];
NSMutableParagraphStyle *paragStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];   
[paragStyle setAlignment:NSCenterTextAlignment]; 
NSDictionary *dictBoldAttr = [NSDictionary dictionaryWithObjectsAndKeys:paragStyle, NSParagraphStyleAttributeName, fontBolded, NSFontNameAttribute, nil];
NSString *sHelloUser = NSLocalizedString(@"Hello User", @"Hello User");
NSAttributedString *attrsHelloUser = [[NSAttributedString alloc] initWithString: sHelloUser attributes:dictBoldAttr];
[self.fooController.tfPanelCenteredField setAttributedStringValue:attrsHelloUser];  
[attrsHelloUser …
Run Code Online (Sandbox Code Playgroud)

cocoa nstextfield nsattributedstring

3
推荐指数
1
解决办法
3778
查看次数

如何在NSString中获取子字符串的NSRange?

NSString *str = @" My name is Mike, I live in California and I work in Texas. Weather in California is nice but in Texas is too hot...";
Run Code Online (Sandbox Code Playgroud)

我怎样才能遍历这个NSString并为每次出现的"California"获取NSRange,我想要NSRange,因为我想在NSAttributed字符串中更改它的颜色.

 NSRange range = NSMakeRange(0,  _stringLength);
 while(range.location != NSNotFound)
 {
    range = [[attString string] rangeOfString: @"California" options:0 range:range];


    if(range.location != NSNotFound)
    {

        range = NSMakeRange(range.location + range.length,  _stringLength - (range.location + range.length));


        [attString addAttribute:NSForegroundColorAttributeName value:_green range:range];
    }
}
Run Code Online (Sandbox Code Playgroud)

nsstring uitextview nsattributedstring ios nsrange

3
推荐指数
2
解决办法
3万
查看次数

在iOS6中打破了TTTAttributedLabel linebreakmode

如果我在TTTAttributedLabel示例应用程序"Espresso"中将linebreakmode设置为截断尾部,看起来它应该在iOS 5模拟器中,但如果我在iOS 6模拟器中运行应用程序,文本会在第一行后被截断,尽管文本超过3行.行数设置为零.我错过了什么?在Espresso应用程序中我注意到这种行为后,我可以不再担心我自己的应用程序中代码的破坏.有什么建议?谢谢!

truncation nsattributedstring ios

3
推荐指数
1
解决办法
1930
查看次数

"NSAttributedString initWithData:options:documentAttribute:error:"无法按预期工作

我正在尝试使用HTML字符串填充UITableView.获取字符串,将它们放入单元格等等一切都很好.我想改变我的NSAttributedText的字体和字体大小.我写了下面的代码.

我通过第一个NSLog检查了代码,如果我的UIFont按预期在字典中; 所以它在字典里.还是没问题.但我的字体和字体大小在运行时没有变化.没有控制台错误.只是不工作.

第二个NSLog用于检查attributionString的字体和大小.它说,我的字符串是"Times New Roman",fontSize:12.但正如您将在代码中看到的那样,我正试图将它变成尺寸为15的"Verdana"

任何人都可以帮我这个吗?谢谢.

PS:我把弦放在一些物体上.obj.name是我想写入表格单元格的字符串.
Html字符串有一些不需要的标记,因此函数[self clearTheHtml:obj.name]函数正在清除它们.

    NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init];
    UIFont *font = [UIFont fontWithName:@"Verdana" size:15];
    [attributes setObject:font forKey:NSFontAttributeName];
    NSString *s = [self clearTheHtml:obj.name];
    NSLog(@"%@", [attributes objectForKey:NSFontAttributeName]); //to see if my font is not in the dictionary. 
    NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[s dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:&attributes error:nil];
    NSLog(@"%@", attributedString);

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2 forIndexPath:indexPath];
    cell.textLabel.attributedText = attributedString;
Run Code Online (Sandbox Code Playgroud)

编辑:

这是NSString*s:

2014-01-25 07:38:04.527 KadirB[7483:70b] 
''Narakas 2013 …
Run Code Online (Sandbox Code Playgroud)

iphone fonts nsattributedstring ios ios7

3
推荐指数
1
解决办法
7522
查看次数

NSAttributedString boundingRectWithSize为表格单元格返回了错误的高度?

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   NSAttributedString *aString = [self.rows objectAtIndex:indexPath.row];

   CGRect r = [aString boundingRectWithSize:CGSizeMake(self.tableView.bounds.size.width, CGFLOAT_MAX)
                                 options:NSStringDrawingUsesLineFragmentOrigin context:nil];

   r.size.height += 50.0f;
    return r.size.height;
}
Run Code Online (Sandbox Code Playgroud)

我的self.rows包含NSAttributedString,它们具有不同的大小.有些只包含一个单词,有些由几行组成(包含换行符)我需要将50.0f添加到高度,否则我的单元格不会显示较长文本的所有内容.

问题是,如果文本只包含一个单词,我会得到很多填充.我的表格单元格只包含一个带有以下自动布局的标签:

@"V:|[customLabel]|"
@"H:|-[_customLabel]-|"
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

uitableview nsattributedstring ios ios7

3
推荐指数
1
解决办法
3767
查看次数

向NSAttributedString添加常规操作?

我只是想在我的UILabel中使用NSAttributedString添加一个objetive-c动作来调用某个范围.

但我不知道该怎么做.我不想打开URL,我只是想调用一个方法.

谢谢

nsattributedstring uilabel ios

3
推荐指数
1
解决办法
4243
查看次数

在textview iOS中的属性字符串末尾添加一个nsstring

我有一个像" Hello There "这样的文本,它将是一个红色的属性字符串.然后在该属性字符串的末尾,我想附加一个nsstring'你是谁?' 但每当我附加一个nsstring时,整个字符串就变成了普通的nsstring,并且删除了属性字符串的属性.我到目前为止的尝试:

NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:@"Hello There"];
[attributeString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,[attributeString length])];

NSMutableAttributedString *previousAttrString = [[NSMutableAttributedString alloc] initWithString:messageTextView.text];

[previousAttrString insertAttributedString: attributeString atIndex:location];
messageTextView.attributedText = previousAttrString;
messageTextView.font = [UIFont fontWithName:@"Helvetica" size:15];

NSString *messageWithContact = [NSString stringWithFormat: @"%@ %@", messageTextView.text, @"Who are you?"];
messageTextView.text=messageWithContact;
Run Code Online (Sandbox Code Playgroud)

我做错了什么?请帮我.

objective-c nsstring nsattributedstring ios

3
推荐指数
1
解决办法
5207
查看次数

无法使用类型为"NSAttributedStringKey"的索引下标"[String:AnyObject]"类型的值

我在我的项目中使用下面的代码.更新到swift 4后,我收到错误.我该如何解决?

码:

let returnString : NSAttributedString

   if styleList.count > 0
   {
       var attrs = [String:AnyObject]()
       attrs[NSAttributedStringKey.font] = codeFont
       for style in styleList
       {
         if let themeStyle = themeDict[style]
         {
             for (attrName, attrValue) in themeStyle
             {
                 attrs.updateValue(attrValue, forKey: attrName)
             }
         }
     }

     returnString = NSAttributedString(string: string, attributes:attrs )
 }
Run Code Online (Sandbox Code Playgroud)

这是错误:


无法使用类型为"NSAttributedStringKey"的索引下标"[String:AnyObject]"类型的值


无法将'[String:AnyObject]'类型的值转换为预期的参数类型'[NSAttributedStringKey:Any]?'

nsattributedstring swift swift4 xcode9 nsattributedstringkey

3
推荐指数
1
解决办法
5417
查看次数