通过UISlider更改UITextView的字体大小

时间:2016-04-21 12:47:59

标签: ios objective-c uitextview uislider

我需要根据UITextView的值更改UISlider的字体大小。

写于viewDidLoad()

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

经过:

  1. UITextViewUISlider的插座均已连接。
  2. UITextVIew行为“可选择”在插座中检查。
  3. 给定UISlider最小值为13,最大值为25英寸 出口。
  4. 编辑: UIView

    的结构

    enter image description here

    我试过以下代码:

    尝试1:

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

    尝试2:

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

    尝试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];
        });
    }
    

    尝试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];
        });
    }
    

    尝试5:

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

    现在有趣的一点是,当我提供其他文字时,它正在工作,即

    工作但不同的小文字:

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

    任何人都可以告诉我为什么这些不起作用?

2 个答案:

答案 0 :(得分:1)

试试这个......

首先设置字体然后再写文字......

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

答案 1 :(得分:0)

所以我尝试过这个有趣的伎俩(很棒,它正在发挥作用):

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

}

但我不知道它为什么会起作用!!