标签: nsattributedstring

iPhone上的CoreText和删除线

我目前正在努力解决在许多人中显示删除线文本的需要UITableViewCell.用HTML编写的东西就像是一样

<strike>€99</strike> save 50% => now €49

我不想只使用UIWebView一行文本,特别是它用于小UITableViewCells.我知道有可重复使用的单元格,但是我希望保持内存效率更高的方式.

所以...我使用的是NSAttributedStringS,与AliSoftware的的帮助UILabel-更换OHAttributedLabel.事实上它只能从iOS 4.0开始使用是没有问题的,因为我们使用的只是4.0兼容的各种东西.

我可以设法创建属性字符串,它显示文本OHAttributedLabel,好的,这很酷.但我无法实现的是设置"删除线"或"删除线"属性.

基本上我是这样的:

NSString *price = [NSString stringWithFormat:@"%01.2f €", product.price];
NSString *rate = [NSString stringWithFormat:@" -%01.0f%%", product.reductionRate];

NSMutableAttributedString *s = [NSMutableAttributedString attributedStringWithString:price];

[s addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlinePatternSolid | NSUnderlineStyleSingle] range:NSRangeFromString(price)];
[attributedLabel setAttributedText:s];

但是在这里,三个NS*常数是未定义的.我进口了CoreText.h,Foundation/NSAttributedString.h但无济于事.我在什么地方看到,在网络上 NSStrikethroughStyleAttributeName = @"NSStrikethroughStyleAttributeName",并且NSUnderlinePatternSolid = 0和NSUnderlineStyleSingle = 1,但硬编码这些值不放弃任何东西.有一件事我与自动完成得到的都是等价kCT...*的常量,但也有kCTUnderlineStyleAttributeName,kCTStrokeWidthAttributeName,...但没有提及 …

iphone nsattributedstring strikethrough core-text ios

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

NSAttributedString - enumerateAttributesInRange导致崩溃?

该方法enumerateAttributesInRange获取一个代码块并为其中的每个属性执行它NSAttributedString

  • 它是异步调用bock吗?

当以下方法在我的应用程序被冻结之后连续两次被调用时,我很想知道它是因为enumerateAttributesInRange异步运行代码块,所以2个线程正在尝试同时修改我的AttributedString.

- (void) doSomething
{
   //following line works fine
   [self doSomethingwithAttributedString];

   //following line works fine
   [self doSomethingwithAttributedString];
   [self performSelector:@selector(doSomethingwithAttributedString) withObject:nil afterDelay:1];

   //following crashes
    [self doSomethingwithAttributedString];
    [self doSomethingwithAttributedString];
}

- (void)doSomethingwithAttributedString
{
   [self.attributedString enumerateAttributesInRange:_selectedRange options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:
 ^(NSDictionary *attributes, NSRange range, BOOL *stop) {

          // Here I modify the dictionary and add it back to my attributedString
 }];
}
Run Code Online (Sandbox Code Playgroud)

iphone objective-c nsattributedstring

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

NSAttributedString initWithData:options:documentAttributes:error:执行速度非常慢

我正在尝试新的iOS 7 API,以下代码需要花费一分钟才能执行!

NSLog(@"start encoding");
NSString *htmlBody = @"<html><body>I am <b>bold</b> and <i>italic</i>";
NSStringEncoding encoding = NSUnicodeStringEncoding;
NSData *data = [htmlBody dataUsingEncoding:encoding];
NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                          NSCharacterEncodingDocumentAttribute: @(encoding)};

NSAttributedString *body = [[NSAttributedString alloc] initWithData:data
                          options:options
                          documentAttributes:nil
                          error:nil];

NSLog(@"end encoding%@", body);
Run Code Online (Sandbox Code Playgroud)

到底是怎么回事?

webkit nsattributedstring ios

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

如何将富文本粘贴到UITextView中?

我的应用程序允许用户通过单击一些格式按钮来格式化UITextView中的文本,这些按钮将属性应用于文本视图的attributedText属性.我想允许用户从一个UITextView复制他们的格式化文本,并使用标准剪贴板和标准剪切/复制/粘贴菜单将其粘贴到另一个.

目前,如果我从UITextView复制格式化文本并将其粘贴到Mail应用程序中的新邮件中,则格式化将保留 - 因此格式化文本的复制将自动进行.但是,如果我将格式化文本粘贴到我的应用程序中的另一个UITextView中,则仅显示纯文本.

通过遵循iOS文本编程指南中的"粘贴选择"部分,我能够覆盖UITextView的粘贴方法并拦截粘贴的内容:

- (void)paste:(id)sender {
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    NSLog(@"types available: %@", [pasteboard pasteboardTypes]);
    for (NSString *type in [pasteboard pasteboardTypes]) {
        NSLog(@"type %@ (%@): %@", type, NSStringFromClass([[pasteboard valueForPasteboardType:type] class]), [pasteboard valueForPasteboardType:type]);
    }
}
Run Code Online (Sandbox Code Playgroud)

这告诉我,粘贴板包含以下格式的内容:com.apple.rtfd,public.rtf,public.text和"Apple Web Archive粘贴板类型".文本的值是纯文本字符串,rtf的值是RTF字符串,两种Apple格式的值是NSData.

这就是我被困住的地方.如何将其中一个项目从粘贴板数据转换为为UITextView设置的属性字符串?

或者更好的是,有没有办法配置UITextView在粘贴时自动接受格式化文本,就像它在复制时自动提供格式化文本一样?

copy-paste uitextview nsattributedstring richtext ios

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

NSAttributedString:未应用下划线

我正在尝试添加下划线样式UITextView,但它没有应用.如果我使用"阴影"(取消注释阴影样式和注释下划线样式)我可以看到它,但由于某种原因没有应用下划线.我使用"Courier New"字体.

- (void) addDiagHighlighting: (NSMutableAttributedString*)attrString start:(int)start end:(int)end severity:(int)severity {
    // ignore diags that are out of bounds
    if (start > attrString.length || end > attrString.length)
        return;

    NSRange range = NSMakeRange(start, end - start);
    UIColor *diagColor = [self getSeverityColor: severity];

    // shadow
//    NSShadow *shadow = [[NSShadow alloc] init];
//    [shadow setShadowColor: diagColor];
//    [shadow setShadowOffset: CGSizeMake (1.0, 1.0)];
//    [shadow setShadowBlurRadius: 1.0];
//    [attrString addAttribute:NSShadowAttributeName
//                       value:shadow
//                       range:range];

    // underline
    [attrString addAttributes:@{
                                NSUnderlineColorAttributeName : diagColor, // …
Run Code Online (Sandbox Code Playgroud)

underline nsattributedstring ios

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

如何在nstextview中设置文本的背景颜色?

我有一个nstextview,我想知道如何设置它的背景颜色.我尝试过很多东西,但似乎都没有用.他们只是设置实际的文本颜色.我有一个名为的NSColorWell,如果有人想知道的话,会调用colorBoxtextview doc.我使用`[self.colorBox color]来获取NSColorWell中的颜色

cocoa objective-c nsattributedstring

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

拆分归因字符串并保留格式

如何在保持格式化的同时,根据预定义的分隔符获取现有的NSAttributedString并将其划分?看起来componentsSeparatedByString似乎不会在NSAttributedString上运行.

我当前的解决方法在正确的点生成拆分,但只输出NSString.因此失去格式.

NSData *rtfFileData = [NSData dataWithContentsOfFile:path];
NSAttributedString *rtfFileAttributedString = [[NSAttributedString alloc] initWithData:rtfFileData options:@{NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType} documentAttributes:nil error:nil];
NSString *rtfFileString = [rtfFileAttributedString string];
NSString *importSeparator = @"###";
// Wish I could do this
// NSArray *separatedArray = [rtfFileAttributedString componentsSeparatedByString:importSeparatorPref];
NSArray *separatedArray = [rtfFileString componentsSeparatedByString:importSeparatorPref];
NSLog( @"Separated array: %@", separatedArray );
Run Code Online (Sandbox Code Playgroud)

objective-c nsattributedstring ios ios8

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

带有归属字符串的Email Composer

如何通过电子邮件发送属性字符串?

mailComposerVC.setMessageBody(textView.attributedString, isHTML: false)
Run Code Online (Sandbox Code Playgroud)

email nsattributedstring ios swift

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

NSAttributedString组合2个字符串与新行

我正在努力实现这种约会 日期

在集合视图单元格内.所以我在2个单独的字符串中有一天的数字和月份.

var day = example.date
var month = example.month
Run Code Online (Sandbox Code Playgroud)

以及下面的功能我正在改变它们的字体颜色等.

 func formatMonth(fullString: String, fontSize: Double) -> NSMutableAttributedString
{
    let range = (fullString as NSString).range(of: fullString)

    var myMutableString = NSMutableAttributedString()

    myMutableString = NSMutableAttributedString(string: fullString)

    myMutableString.setAttributes([NSFontAttributeName : UIFont(name: "HelveticaNeue-Bold", size: CGFloat(fontSize))!
        , NSForegroundColorAttributeName : UIColor.red], range: range)


    return myMutableString
}
func formatDay(fullString: String, fontSize: Double) -> NSMutableAttributedString
{
    let range = (fullString as NSString).range(of: fullString)

    var myMutableString = NSMutableAttributedString()

    myMutableString = NSMutableAttributedString(string: fullString)

    myMutableString.setAttributes([NSFontAttributeName : UIFont(name: "HelveticaNeue", size: CGFloat(fontSize))!
        , NSForegroundColorAttributeName …
Run Code Online (Sandbox Code Playgroud)

nsattributedstring ios swift swift3

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

NSAttributedString项目符号列表问题

我正在尝试使用NSAttributedString和创建项目符号列表UITextView。而且,这是到目前为止我能够实现的:在此处输入图片说明

可以看到,两行之间有一个小的“偏移”。这是我用来构建属性字符串的代码片段:

func add(bulletList strings: [String],
         indentation: CGFloat = 15,
         lineSpacing: CGFloat = 3,
         paragraphSpacing: CGFloat = 10) {

    func createParagraphAttirbute() -> NSParagraphStyle {
        var paragraphStyle: NSMutableParagraphStyle
        let nonOptions = NSDictionary() as! [NSTextTab.OptionKey: Any]

        paragraphStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
        paragraphStyle.tabStops = [
            NSTextTab(textAlignment: .left, location: indentation, options: nonOptions)]
        paragraphStyle.defaultTabInterval = indentation
        paragraphStyle.firstLineHeadIndent = 0
        paragraphStyle.lineSpacing = lineSpacing
        paragraphStyle.paragraphSpacing = paragraphSpacing
        paragraphStyle.headIndent = indentation
        return paragraphStyle
    }

    var buffer = NSMutableAttributedString.init()

    for string in strings {
        let …
Run Code Online (Sandbox Code Playgroud)

uitextview nsattributedstring ios swift

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