如何在 iOS 中设置文本格式
How to set text formatting in iOS
问题:
我可以在iOS中设置文字,就像下面的图片一样吗?任何帮助将不胜感激。提前致谢。
是的,你可以。
您必须像往常一样创建一个 UITextView
并将图像添加为 UIImageView
中的子视图。
yourTextView = [[UITextView alloc] init];
yourTextView.frame = CGRectMake(0, 10, self.view.frame.size.width, self.view.frame.size.height);
yourTextView.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:20];
yourTextView.text = @"The year was 1572. Pratap Singh had just become the Maharana of Mewar and he had not been back in Chittor since 1567. His old fort and his home beckoned to him. The pain of his father's death, and the fact that his father had not been able to see Chittor again, troubled the young Maharana deeply. But he was not the only one troubled at this time. Akbar had control of Chittor but not the kingdom of Mewar. So long as the people of Mewar swore by their Maharana, Akbar could not realize his ambition of being the Jahanpanah of Hindustan.";
[self.view addSubview:yourTextView];
yourImageView = [[UIImageView alloc] init];
yourImageView.frame = CGRectMake(100, 100, self.view.frame.size.width-100, (self.view.frame.size.width-100)*1.5 );
yourImageView.image = [UIImage imageNamed:@"imageOfAnOldSamurai.png"];
[yourTextView addSubview:yourImageView];
要使 textview 内的文本环绕图像,请使用 exclusionPaths
,这是一个 UIBezierPath
和来自 imageview 的框架。
- (void)viewDidLayoutSubviews {
UIBezierPath * exclusionPath = [UIBezierPath bezierPathWithRect:yourImageView.frame];
yourTextView.textContainer.exclusionPaths = @[exclusionPath];
}
上面的代码为我创建了这个结果:
问题:
我可以在iOS中设置文字,就像下面的图片一样吗?任何帮助将不胜感激。提前致谢。
是的,你可以。
您必须像往常一样创建一个 UITextView
并将图像添加为 UIImageView
中的子视图。
yourTextView = [[UITextView alloc] init];
yourTextView.frame = CGRectMake(0, 10, self.view.frame.size.width, self.view.frame.size.height);
yourTextView.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:20];
yourTextView.text = @"The year was 1572. Pratap Singh had just become the Maharana of Mewar and he had not been back in Chittor since 1567. His old fort and his home beckoned to him. The pain of his father's death, and the fact that his father had not been able to see Chittor again, troubled the young Maharana deeply. But he was not the only one troubled at this time. Akbar had control of Chittor but not the kingdom of Mewar. So long as the people of Mewar swore by their Maharana, Akbar could not realize his ambition of being the Jahanpanah of Hindustan.";
[self.view addSubview:yourTextView];
yourImageView = [[UIImageView alloc] init];
yourImageView.frame = CGRectMake(100, 100, self.view.frame.size.width-100, (self.view.frame.size.width-100)*1.5 );
yourImageView.image = [UIImage imageNamed:@"imageOfAnOldSamurai.png"];
[yourTextView addSubview:yourImageView];
要使 textview 内的文本环绕图像,请使用 exclusionPaths
,这是一个 UIBezierPath
和来自 imageview 的框架。
- (void)viewDidLayoutSubviews {
UIBezierPath * exclusionPath = [UIBezierPath bezierPathWithRect:yourImageView.frame];
yourTextView.textContainer.exclusionPaths = @[exclusionPath];
}
上面的代码为我创建了这个结果: