Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to use NSAttributedString in UITextView?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 446
    Comment on it

    We can use attributed string with textView by following steps -

     

    1. Make a IBOutlet of your textView
    for eg -

    @property (weak, nonatomic) IBOutlet UITextView *textView;

     

    2. Now go to storyboard and select your textView and change “Text” form “Plain” to “Attributed”.

     

    3. Now come to your viewController’s .m class, and set string for your textView like -

    NSString *dummyString = @"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim ";

     

    4. Make a function to give all desired Properties to your text of textView.

    -(void)setAttributedTextInTextView:(NSString*)dummyString{
        // this line of code will set your text alignment RightToLeft and also will set spacing between line of textView(you can change it according to your preferences).
    
        NSMutableParagraphStyle *paragraphStyle = NSMutableParagraphStyle.new;
        paragraphStyle.alignment                = NSTextAlignmentRight;
        paragraphStyle.lineSpacing = 5;
        // here we are creating a dictionary to give attributes to text of text view.
        NSDictionary *attrDict = @{
                                   NSFontAttributeName : [UIFont fontWithName:@"Arial" size:13.0],// this will give font and its size to your textViews text.
                                   NSParagraphStyleAttributeName:paragraphStyle, // here we set paragraph style define above.
                                   NSBackgroundColorAttributeName:[UIColor blackColor],//this will set color of textView background.
                                   NSForegroundColorAttributeName:[UIColor whiteColor],// this will set color of text in your textView.
                                   NSKernAttributeName:@4,// this will create space between each character of textViews text.
                                   };
        
        // here we are giving the string and its all attribute to attributed string.
        NSAttributedString *attributedDescriptionStr = [[NSAttributedString alloc] initWithString:dummyString attributes: attrDict];
    
        // by this line text field will be automatically at the top of textView.
        [self.textView scrollRangeToVisible:NSMakeRange(0, 0)];
        self.textView.attributedText = attributedDescriptionStr;
    }
    

     

    5. Call this function from viewDidLoad or wherever you want to use it.

    - (void)viewDidLoad {
        [super viewDidLoad];
        NSString *dummyString = @"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim ";
        [self setAttributedTextInTextView:dummyString];
        // Do any additional setup after loading the view, typically from a nib.
    }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: