【问题标题】:How to add attributes to an Empty TextView?如何向空的 TextView 添加属性?
【发布时间】:2014-04-02 14:02:09
【问题描述】:

在我的应用程序中,我有一个 UITextView,而在 inputAccessory 视图中,我有一些按钮可以让用户输入粗体、斜体等。

当我点击粗体按钮时,我正在添加粗体字体的属性,但 UItextView 中没有文本,因此所选范围始终为 (0,0)。我应该使用什么作为选定的范围,以便我可以用粗体输入即将到来的文本。当我再次点击粗体按钮时,它应该取消粗体字体属性并让用户再次输入普通字体。 这是我正在使用的代码:

-(void)addOrRemoveFontTraitWithName:(NSString *)traitName andValue:(uint32_t)traitValue {

    NSRange selectedRange = [self.commentsTextView selectedRange];


    NSDictionary *currentAttributesDict;
    if (selectedRange.location==0 && selectedRange.length==0) {
        //currentAttributesDict = [self.commentsTextView.textStorage attributesAtIndex:selectedRange.location effectiveRange:nil];
    }
    else{
        currentAttributesDict = [self.commentsTextView.textStorage attributesAtIndex:selectedRange.location
                                                                                    effectiveRange:nil];
    }

    UIFont *currentFont;
    if (currentAttributesDict.count >0) {
        currentFont = [currentAttributesDict objectForKey:NSFontAttributeName];
    }
    else{
        currentFont = [UIFont fontWithName:@"Sans-Regular" size:20.0f];
    }
    //currentFont = [currentAttributesDict objectForKey:NSFontAttributeName];
    UIFontDescriptor *fontDescriptor = [currentFont fontDescriptor];

    NSString *fontNameAttribute = [[fontDescriptor fontAttributes] objectForKey:UIFontDescriptorNameAttribute];
    UIFontDescriptor *changedFontDescriptor;

    if ([fontNameAttribute rangeOfString:traitName].location == NSNotFound) {
        uint32_t existingTraitsWithNewTrait = [fontDescriptor symbolicTraits] | traitValue;
        changedFontDescriptor = [fontDescriptor fontDescriptorWithSymbolicTraits:existingTraitsWithNewTrait];
    }
    else{
        uint32_t existingTraitsWithoutTrait = [fontDescriptor symbolicTraits] & ~traitValue;
        changedFontDescriptor = [fontDescriptor fontDescriptorWithSymbolicTraits:existingTraitsWithoutTrait];
    }

    UIFont *updatedFont = [UIFont fontWithDescriptor:changedFontDescriptor size:0.0];

    NSDictionary *dict = @{NSFontAttributeName: updatedFont};

    /*NSMutableDictionary * combined = [[NSMutableDictionary alloc]init];
    [combined setObject:updatedFont forKey:NSFontAttributeName];
    [combined setObject:[NSNumber numberWithInt:1] forKey:NSUnderlineStyleAttributeName];*/
    NSRange newRange = NSMakeRange(0, 10000);
    [self.commentsTextView.textStorage beginEditing];
    [self.commentsTextView.textStorage setAttributes:dict range:newRange];
    [self.commentsTextView.textStorage endEditing];
}

【问题讨论】:

  • 你想给一个等于nil的字符串添加属性吗?
  • 你能解释一下吗?
  • 如果字符串的长度为 0,则无法添加属性。

标签: ios objective-c uitextview nsmutableattributedstring nstextstorage


【解决方案1】:

我想使用setTypingAttributes: 可能会对您有所帮助。 here也是一样的解释。

【讨论】:

  • 知道了!!!但是我如何向字典添加多个属性。在一种情况下,如果用户同时按下粗体和斜体按钮,则文本应同时显示为粗体和斜体。如果用户选择了所有 3 个粗斜体和下划线,情况也是如此。
  • 这很好。请检查以下链接。 code4app.net/ios/Rich-Text-View/51cd08686803face6a000000
  • 我现在没有 Mac 机器来检查该应用程序。所以我想这个链接可能会对你有所帮助。
  • 不准确。这是基于 HTMl 的,我正在尝试使用 TextKit 来实现这一目标
  • 所以这正是您正在寻找的。 appcoda.com/intro-text-kit-ios-programming-guide
猜你喜欢
  • 2015-06-22
  • 1970-01-01
  • 2017-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-14
  • 2010-11-21
相关资源
最近更新 更多