简体   繁体   中英

Changing font size of UITextView by UISlider

I need to change the UITextView 's font Size according to the value of UISlider .

Written in viewDidLoad()

tvVarse.text=strTextToShow; //tvVarse is the UITextViewOutlet, strTextToShow is the text to be shown on textView

Checked:

  1. Both UITextView and UISlider 's Outlet is Connected.
  2. UITextVIew Behavior "Selectable" is checked in outlet.
  3. Given UISlider Minimum Value is 13 and Maximum Value is 25 in Outlet.

EDIT: Structure of UIView

在此处输入图片说明

I have tried following codes:

Try 1:

- (IBAction)sliderTap:(UISlider*)sender //Value changed action for UISlider
{
    tvVarse.font = [tvVarse.font fontWithSize:sender.value];
}

Try 2:

- (IBAction)sliderTap:(UISlider*)sender //Value changed action for UISlider
{
    dispatch_async(dispatch_get_main_queue(), ^{

    tvVarse.font = [tvVarse.font fontWithSize:sender.value];

    });
}

Try 3:

- (IBAction)sliderTap:(UISlider*)sender //Value changed action for UISlider
{
    dispatch_async(dispatch_get_main_queue(), ^{
    tvVarse.text=strTextToShow;
    tvVarse.font = [tvVarse.font fontWithSize:sender.value];
    [tvVarse setNeedsLayout];
    [tvVarse setNeedsDisplay];
    });
}

Try 4:

- (IBAction)sliderTap:(UISlider*)sender //Value changed action for UISlider
{
    dispatch_async(dispatch_get_main_queue(), ^{
    tvVarse.font = [tvVarse.font fontWithSize:sender.value];
    tvVarse.text=strTextToShow;
    [tvVarse setNeedsLayout];
    [tvVarse setNeedsDisplay];
    });
}

Try 5:

- (IBAction)sliderTap:(UISlider*)sender //Value changed action for UISlider
{
    tvVarse.font = [tvVarse.font fontWithSize:sender.value];
    tvVarse.text=strTextToShow;
}

Now the funny Point is When I am Providing Other text, it is Working ie

Working but different small text:

- (IBAction)sliderTap:(UISlider*)sender //Value changed action for UISlider
{
    tvVarse.text=@"My name is Manab";
    tvVarse.font = [tvVarse.font fontWithSize:sender.value];
}

Can anyone please tell me why these are not working?

Try this...

First set the font and then write text...

tvVarse.font = [tvVarse.font fontWithSize:sender.value];
tvVarse.text=strTextToShow;

So I have tried with this funny Trick (Wonderfully it is working):

- (IBAction)sliderTap:(UISlider*)sender
{
    tvVarse.text=@"";//Just provided blank text before loading textview again
    tvVarse.font = [tvVarse.font fontWithSize:sender.value];
    tvVarse.text=strTextToShow;

}

But I don't know why it is working!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM