标签: nsattributedstring

将文本设置为不带选择的下划线

我试图让用户能够设置他们将键入下划线的文本,而不会选择当前的文本.这适用于iOS 6应用程序,在UITextView中输入文本.它将保存为NSAttributedString.大胆和斜体很好.关于下划线的一些事情是让它无法工作.

UITextView *textView = [self noteTextView];
NSMutableDictionary *typingAttributes = [[textView typingAttributes] mutableCopy];
[typingAttributes setObject:[NSNumber numberWithInt:NSUnderlineStyleSingle] forKey:NSUnderlineStyleAttributeName];
NSLog(@"attributes after: %@", typingAttributes);
[textView setTypingAttributes:typingAttributes];
NSLog(@"text view attributes after: %@", [textView typingAttributes]);
Run Code Online (Sandbox Code Playgroud)

我的初始日志语句表明它设置为下划线:

attributes after: {
    NSColor = "UIDeviceRGBColorSpace 0 0 0 1";
    NSFont = "<UICFFont: 0xa9c5e30> font-family: \"Verdana\"; font-weight: normal; font-style: normal; font-size: 17px";
    NSKern = 0;
    NSStrokeColor = "UIDeviceRGBColorSpace 0 0 0 1";
    NSStrokeWidth = 0;
    NSUnderline = 1;
}
Run Code Online (Sandbox Code Playgroud)

但紧接着的日志语句不显示nsunderline属性.删除textView setTypingAttributes行没有任何影响.

text view attributes after: {
    NSColor = "UIDeviceRGBColorSpace …
Run Code Online (Sandbox Code Playgroud)

underline nsattributedstring ios ios6

5
推荐指数
1
解决办法
3816
查看次数

为什么NSLineBreakByWordWrapping分裂单词?

使用NSAttributedString,我尝试在三角形内创建文本,如下所示;

http://postimg.org/image/rp9fukgaj/

我使用"NSLineBreakByWordWrapping"方法在定义的形状内拟合文本.但是第一个文字分裂,我无法找到解决方案.我希望我的文本跳转到以下行而不是分裂.我怎样才能做到这一点?

在我使用的代码下面;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor blackColor];
    CTFontRef fontRef = CTFontCreateWithName((CFStringRef)@"HelveticaNeue-Regular", 15.0f, NULL);     
    NSMutableParagraphStyle* paragraph = [NSMutableParagraphStyle new];
    paragraph.headIndent = 10;
    paragraph.firstLineHeadIndent = 10;
    paragraph.tailIndent = -10;
    paragraph.lineBreakMode = NSLineBreakByWordWrapping;
    paragraph.alignment = NSTextAlignmentCenter;
    paragraph.paragraphSpacing = 15;    
    NSDictionary *attrDictionary = [NSDictionary dictionaryWithObjectsAndKeys: (__bridge id)fontRef, (NSString *)kCTFontAttributeName, (id)[[UIColor whiteColor] CGColor], (NSString *)(kCTForegroundColorAttributeName),paragraph,NSParagraphStyleAttributeName, nil];    
    CFRelease(fontRef);
    NSAttributedString *attString = [[NSAttributedString alloc] initWithString:@"Initially, now I create the paragraph style and apply it to the first character. Note how the margins are …
Run Code Online (Sandbox Code Playgroud)

line-breaks nsattributedstring ios

5
推荐指数
1
解决办法
517
查看次数

可能在归属文本背景上有圆角吗?

我将Text归为不同的文本颜色.为了更好地阅读突出显示的文本,我使用backgroundcolor.是否有可能在这个Textarea上带来圆角的CALayer效果?不是整个区域,而是内部的特殊文本.

我的代码:

NSAttributedString *text = [[NSAttributedString alloc] initWithString:@"Highlighted Text"
                                                                          attributes:@{
                                                                                       NSFontAttributeName:            [UIFont fontWithName:@"HelveticaNeue" size:10.0f],
                                                                                       NSForegroundColorAttributeName: [UIColor lightGrayColor],
                                                                                       NSBackgroundColorAttributeName: [UIColor darkGrayColor]
                                                                                       }];
Run Code Online (Sandbox Code Playgroud)

iphone nsattributedstring uilabel ios quartz-core

5
推荐指数
0
解决办法
5675
查看次数

后台线程中来自HTML的NSAttributedString

我需要的是NSAttributedString从相对较大的HTML字符串创建对象并将它们(NSAttributedString-s)存储在数据库中.当然我想在后台线程中这样做.

这是一个简单的代码(失败)来演示我正在尝试做的事情:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    NSString *HTMLString = @"<p>HTML string</p>";
    NSDictionary *options = @{NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute : @(NSUTF8StringEncoding)};
    NSError *error = nil;
    NSAttributedString *attributedText = [[NSAttributedString alloc] initWithData:[HTMLString dataUsingEncoding:NSUTF8StringEncoding] options:options documentAttributes:nil error:&error];
});
Run Code Online (Sandbox Code Playgroud)

根据这个讨论,可能根本不可能在后台线程中执行它,因为NSAttributedString使用NSHTMLTextDocumentType选项创建WebKit需要在执行任何异步工作时旋转runloop.

但也许有人想通了这条路?我有非常简单的HTML,只是文本和链接,可能有一种方法来指定NSAttributedString从HTML 创建一个不需要任何" WebKit-rendering"?

或者可能有人可以推荐第三方lib NSAttributedString从不使用的简单HTML 创建s WebKit?

html nsattributedstring ios ios7

5
推荐指数
1
解决办法
5348
查看次数

带文字附件和截断的属性字符串

我有一个附有图片的属性字符串(NSTextAttachment)。一切正常,但是我遇到了无法解决的截断问题。

在示例中,假设字符串##是图像。所以我的弦看起来像Hello world! ##。在段落样式上设置了尾部截断。

现在,如果空间受到限制,则文本将被省略号省略(这是我想要的)。但不幸的是,图像也被截断了。

所以结果是这样的:

Hello w...
Run Code Online (Sandbox Code Playgroud)

但我希望它看起来像:

Hello...##
Run Code Online (Sandbox Code Playgroud)

也就是说,我希望图像附件不会被截断,它应该始终可见。

附件的原因是我希望图像始终位于字符串的末尾,因此,当文本较短时,图像位于末尾,并且当文本换行时,我也希望图像位于字符串末尾结束。尝试手动将图像“放在外面”将不起作用,因为文本无法正确地被截断。

因此,有没有办法告诉您NSAttributedString不要截断图像?

产生属性字符串的示例代码:

NSString *title;
NSMutableAttributedString *attributedString;
NSMutableParagraphStyle *paragraph;
NSDictionary *attributes;
NSTextAttachment *attachment;

paragraph = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
paragraph.hyphenationFactor = 1.0;
paragraph.lineBreakMode = NSLineBreakByTruncatingTail;

attributes = @{
    NSForegroundColorAttributeName : [self titleTextColor],
    NSParagraphStyleAttributeName : paragraph,
};

title = @"Hello world!";
attributedString = [[NSMutableAttributedString alloc] initWithString:title
                                                          attributes:attributes];

attachment = [[NSTextAttachment alloc] init];
attachment.image = [UIImage imageNamed:@"myImage"];
[attributedString appendAttributedString:[NSAttributedString attributedStringWithAttachment:attachment]];
[attachment release];

self.titleLabel.attributedText = …
Run Code Online (Sandbox Code Playgroud)

objective-c nsattributedstring nstextattachment

5
推荐指数
1
解决办法
4548
查看次数

如何使用不同的字体在NSAttributedString中垂直居中一行?

我有一个NSAttributedString"表"使用NSTextTab停止,其中第一列是KEY,第二列是值

像这样:

|  **KEY 1**|value1|
|**KEY TWO**|value2|
Run Code Online (Sandbox Code Playgroud)

KEYS的字体大小/重量不同+全部大写.行对齐,以便所有字母的底部匹配.我想垂直微调这些值,使它们看起来与KEYS垂直居中.

有没有办法做到这一点?这似乎非常专业,但从设计的角度来看,我认为它看起来会更好

objective-c nsattributedstring core-text ios nsparagraphstyle

5
推荐指数
1
解决办法
2761
查看次数

EffectiveRange和LongestEffectiveRange之间的区别

在NSAttributedString类中,有些函数可以获取某些索引和范围的属性值,但我不确定选择器之间的区别attributesAtIndex:effectiveRange,attributesAtIndex:longestEffectiveRange:inRange:以及何时使用一个而不是另一个?提前感谢您的任何澄清

attributes objective-c range nsattributedstring

5
推荐指数
1
解决办法
984
查看次数

如何停止枚举NSAttributedString的属性?

在该enumerateAttribute方法的文档中,关于stop块的参数,它说:

该块可以将值设置为true以停止对集合的进一步处理.

但是,在块内部,stop参数是a let,我无法将其设置为true.

在找到第一个属性后,我需要停止枚举.我怎么能这样做?

enumeration nsattributedstring ios swift

5
推荐指数
1
解决办法
720
查看次数

如何为`UILabel`设置字符(kern)和删除线样式之间的间距?

我需要为一个文本设置两个属性UILabel:a .字母间隔(kern)和它的删除线样式.根据NSAttributedStringKey文档,我创建了以下扩展UILabel:

extension UILabel {
    func setStrikeThroughSpacedText(text: String, kern: CGFloat?) {
        var attributes: [NSAttributedStringKey : Any] = [:]
        if let kern = kern {
            attributes[.kern] = kern
        }
        attributes[.strikethroughStyle] 
                  = NSNumber(integerLiteral: NSUnderlineStyle.styleSingle.rawValue)
        self.attributedText = NSAttributedString(string: text,
                                                 attributes: attributes)
    }
}
Run Code Online (Sandbox Code Playgroud)

然而,似乎.kern键以某种方式与键碰撞.strikethroughStyle,因为如果我指定kern,则应用kern,但不应用删除线样式.如果我没有指定kern(因此扩展名不应用该.kern属性),删除线样式将起作用.

任何人都有不同的方法来解决这个错误(我认为这是一个错误)?

nsattributedstring ios swift ios10 swift4

5
推荐指数
1
解决办法
2647
查看次数

NSAttributedStringKey.foregroundColor无效

我正在使用ZSWTappableLabel并ZSWTaggedString在标签内显示链接.

import ZSWTappableLabel
import ZSWTaggedString
Run Code Online (Sandbox Code Playgroud)

pod版本是:

  pod 'ZSWTappableLabel', '~> 2.0'
  pod 'ZSWTaggedString/Swift', '~> 4.0'
Run Code Online (Sandbox Code Playgroud)

默认情况下,链接过去常常以白色显示(与标签颜色相同),但在最近发生的一些更新(可能是pod更新或xcode版本,我无法准确指出是什么)之后,链接已经开始以蓝色显示.设置NSAttributedStringKey.foregroundColor为白色似乎不会影响任何事情.NSAttributedStringKey.backgroundColor确实会影响它,但由于某种原因,foregroundColor似乎没有任何影响.

如何设置白色链接?

func setTermsAndPrivacyLinkLabel(){
    termsAndPrivacyLabel.tapDelegate = self

    let options = ZSWTaggedStringOptions()
    options["link"] = .dynamic({ tagName, tagAttributes, stringAttributes in
        guard let type = tagAttributes["type"] as? String else {
            return [NSAttributedStringKey : Any]()
        }

        var foundURL: NSURL?

        switch type {
        case "privacy":
            foundURL = NSURL(string: "\(privacyUrl)")!
        case "tos":
            foundURL = NSURL(string: "\(termsUrl)")!
        default:
            break
        }

        guard let URL …
Run Code Online (Sandbox Code Playgroud)

nsattributedstring swift swift4 nsattributedstringkey

5
推荐指数
1
解决办法
255
查看次数