0

In my app i have a UITextView and in the inputAccessory view i have a few buttons which can let the user type in Bold, Italics etc.

When I tap the bold button i am adding the attribute for bold font but there is no text in the UItextView so the selected range is always (0,0). What should i use as the selected Range so that i can enter the coming text in Bold. And when i tap the bold button again it should take away the bold font attribute and the let the user type in normal font again. Here's the code i am using :

-(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];
}
Ashutosh
  • 5,614
  • 13
  • 52
  • 84

1 Answers1

0

I guess using setTypingAttributes: might help you. The same is explained here.

Community
  • 1
  • 1
iOS
  • 3,526
  • 3
  • 37
  • 82
  • Got it !!! but how would i add multiple attributes to the dict. In one case if the user has pressed Bold and italics button together the text should be displayed as both bold and italics. same goes if the user selected all 3 bold italics and underline. – Ashutosh Apr 02 '14 at 18:48
  • Thats a good one. Pls check the below link. http://code4app.net/ios/Rich-Text-View/51cd08686803face6a000000 – iOS Apr 03 '14 at 05:24
  • I do not have a Mac machine to check that app now. So I guess that the link may help you. – iOS Apr 03 '14 at 05:25
  • Not precisely. This is based upon HTMl and i am trying to achieve this with TextKit – Ashutosh Apr 03 '14 at 18:13
  • So this would be exactly what your are looking for. http://www.appcoda.com/intro-text-kit-ios-programming-guide/ – iOS Apr 04 '14 at 06:44
  • Yeah.. i have already gone through this. This would help you to apply attributes to an already entered string after selection but if i just want to click the bold button and then start typing and the text should be typed in bold. This is not achievable through this – Ashutosh Apr 06 '14 at 17:14