- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我只是想创建一个这样的UITextView(不是两个textview,空白处是一个uiimage)
最佳答案
没有内置的 UIView
子类可以执行此操作(如果您编写正确的 HTML 和 CSS,则 UIWebView
除外),但使用 Core Text 很容易做到。我把我的测试项目放在 my ShapedLabel github repository 中, 这是它的样子:
该项目有一个名为 ShapedLabel
的 UIView
子类。这是它的工作原理。
创建一个名为 ShapedLabel
的 UIView
子类。给它这些属性:
@property (nonatomic, copy) NSString *text;
@property (nonatomic) UITextAlignment textAlignment;
@property (nonatomic, copy) NSString *fontName;
@property (nonatomic) CGFloat fontSize;
@property (nonatomic, strong) UIColor *textColor;
@property (nonatomic, strong) UIColor *shapeColor;
@property (nonatomic, copy) UIBezierPath *path;
您需要覆盖每个属性设置方法以发送 setNeedsDisplay
,例如:
- (void)setFontName:(NSString *)fontName {
_fontName = [fontName copy];
[self setNeedsDisplay];
}
我依靠 ARC 担心释放 _fontName
的旧值。如果您不使用 ARC... 开始。它非常简单,并且自 iOS 4.0 起就受支持。
无论如何,您需要实现drawRect:
,真正的工作在这里完成。首先,如果设置了 shapeColor
,我们将填充该形状:
- (void)drawRect:(CGRect)rect
{
if (!_path)
return;
if (_shapeColor) {
[_shapeColor setFill];
[_path fill];
}
我们检查以确保我们拥有我们需要的所有其他参数:
if (!_text || !_textColor || !_fontName || _fontSize <= 0)
return;
接下来我们处理 textAligment
属性:
CTTextAlignment textAlignment = NO ? 0
: _textAlignment == UITextAlignmentCenter ? kCTCenterTextAlignment
: _textAlignment == UITextAlignmentRight ? kCTRightTextAlignment
: kCTLeftTextAlignment;
CTParagraphStyleSetting paragraphStyleSettings[] = {
{
.spec = kCTParagraphStyleSpecifierAlignment,
.valueSize = sizeof textAlignment,
.value = &textAlignment
}
};
CTParagraphStyleRef style = CTParagraphStyleCreate(paragraphStyleSettings, sizeof paragraphStyleSettings / sizeof *paragraphStyleSettings);
接下来我们创建CTFont
。请注意,这不同于 CGFont
或 UIFont
。您可以使用 CTFontCreateWithGraphicsFont
将 CGFont
转换为 CTFont
,但是您无法轻松地将 UIFont
转换为 CTFont
。无论如何,我们只是直接创建 CTFont
:
CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)_fontName, _fontSize, NULL);
我们创建属性字典来定义我们想要看到的所有样式属性:
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
(__bridge id)font, kCTFontAttributeName,
_textColor.CGColor, kCTForegroundColorAttributeName,
style, kCTParagraphStyleAttributeName,
nil];
CFRelease(font);
CFRelease(style);
一旦我们有了属性字典,我们就可以创建属性字符串,将属性字典附加到文本字符串。这是 Core Text 使用的:
CFAttributedStringRef trib = CFAttributedStringCreate(NULL, (__bridge CFStringRef)_text, (__bridge CFDictionaryRef)attributes);
我们创建了一个 Core Text framesetter,它将根据属性字符串布置文本:
CTFramesetterRef setter = CTFramesetterCreateWithAttributedString(trib);
CFRelease(trib);
Core Text 假定图形上下文将具有原点在左下方的“标准”Core Graphics 坐标系。但是 UIKit 更改了上下文以将原点放在左上角。我们假设路径是在考虑到这一点的情况下创建的。所以我们需要一个垂直翻转坐标系的变换:
// Core Text lays out text using the default Core Graphics coordinate system, with the origin at the lower left. We need to compensate for that, both when laying out the text and when drawing it.
CGAffineTransform textMatrix = CGAffineTransformIdentity;
textMatrix = CGAffineTransformTranslate(textMatrix, 0, self.bounds.size.height);
textMatrix = CGAffineTransformScale(textMatrix, 1, -1);
然后我们可以创建路径的翻转副本:
CGPathRef flippedPath = CGPathCreateCopyByTransformingPath(_path.CGPath, &textMatrix);
最后我们可以让框架 setter 布置一个文本框架。这实际上适合 path
属性定义的形状内的文本:
CTFrameRef frame = CTFramesetterCreateFrame(setter, CFRangeMake(0, 0), flippedPath, NULL);
CFRelease(flippedPath);
CFRelease(setter);
最后我们绘制文本。我们需要再次
CGContextRef gc = UIGraphicsGetCurrentContext();
CGContextSaveGState(gc); {
CGContextConcatCTM(gc, textMatrix);
CTFrameDraw(frame, gc);
} CGContextRestoreGState(gc);
CFRelease(frame);
}
差不多就这些了。您现在可以在屏幕上放置一个形状漂亮的标签。
对于后代(以防我删除测试项目),这里是 ShapedLabel
类的完整源代码。
#import <UIKit/UIKit.h>
@interface ShapedLabel : UIView
@property (nonatomic, copy) NSString *text;
@property (nonatomic) UITextAlignment textAlignment;
@property (nonatomic, copy) NSString *fontName;
@property (nonatomic) CGFloat fontSize;
@property (nonatomic, strong) UIColor *textColor;
@property (nonatomic, strong) UIColor *shapeColor;
@property (nonatomic, copy) UIBezierPath *path;
@end
#import "ShapedLabel.h"
#import <CoreText/CoreText.h>
@implementation ShapedLabel
@synthesize fontName = _fontName;
@synthesize fontSize = _fontSize;
@synthesize path = _path;
@synthesize text = _text;
@synthesize textColor = _textColor;
@synthesize shapeColor = _shapeColor;
@synthesize textAlignment = _textAlignment;
- (void)commonInit {
_text = @"";
_fontSize = UIFont.systemFontSize;
// There is no API for just getting the system font name, grr...
UIFont *uiFont = [UIFont systemFontOfSize:_fontSize];
_fontName = [uiFont.fontName copy];
}
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self commonInit];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self commonInit];
}
return self;
}
- (void)setFontName:(NSString *)fontName {
_fontName = [fontName copy];
[self setNeedsDisplay];
}
- (void)setFontSize:(CGFloat)fontSize {
_fontSize = fontSize;
[self setNeedsDisplay];
}
- (void)setPath:(UIBezierPath *)path {
_path = [path copy];
[self setNeedsDisplay];
}
- (void)setText:(NSString *)text {
_text = [text copy];
[self setNeedsDisplay];
}
- (void)setTextColor:(UIColor *)textColor {
_textColor = textColor;
[self setNeedsDisplay];
}
- (void)setTextAlignment:(UITextAlignment)textAlignment {
_textAlignment = textAlignment;
[self setNeedsDisplay];
}
- (void)setShapeColor:(UIColor *)shapeColor {
_shapeColor = shapeColor;
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect
{
if (!_path)
return;
if (_shapeColor) {
[_shapeColor setFill];
[_path fill];
}
if (!_text || !_textColor || !_fontName || _fontSize <= 0)
return;
CTTextAlignment textAlignment = NO ? 0
: _textAlignment == UITextAlignmentCenter ? kCTCenterTextAlignment
: _textAlignment == UITextAlignmentRight ? kCTRightTextAlignment
: kCTLeftTextAlignment;
CTParagraphStyleSetting paragraphStyleSettings[] = {
{
.spec = kCTParagraphStyleSpecifierAlignment,
.valueSize = sizeof textAlignment,
.value = &textAlignment
}
};
CTParagraphStyleRef style = CTParagraphStyleCreate(paragraphStyleSettings, sizeof paragraphStyleSettings / sizeof *paragraphStyleSettings);
CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)_fontName, _fontSize, NULL);
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
(__bridge id)font, kCTFontAttributeName,
_textColor.CGColor, kCTForegroundColorAttributeName,
style, kCTParagraphStyleAttributeName,
nil];
CFRelease(font);
CFRelease(style);
CFAttributedStringRef trib = CFAttributedStringCreate(NULL, (__bridge CFStringRef)_text, (__bridge CFDictionaryRef)attributes);
CTFramesetterRef setter = CTFramesetterCreateWithAttributedString(trib);
CFRelease(trib);
// Core Text lays out text using the default Core Graphics coordinate system, with the origin at the lower left. We need to compensate for that, both when laying out the text and when drawing it.
CGAffineTransform textMatrix = CGAffineTransformIdentity;
textMatrix = CGAffineTransformTranslate(textMatrix, 0, self.bounds.size.height);
textMatrix = CGAffineTransformScale(textMatrix, 1, -1);
CGPathRef flippedPath = CGPathCreateCopyByTransformingPath(_path.CGPath, &textMatrix);
CTFrameRef frame = CTFramesetterCreateFrame(setter, CFRangeMake(0, 0), flippedPath, NULL);
CFRelease(flippedPath);
CFRelease(setter);
CGContextRef gc = UIGraphicsGetCurrentContext();
CGContextSaveGState(gc); {
CGContextConcatCTM(gc, textMatrix);
CTFrameDraw(frame, gc);
} CGContextRestoreGState(gc);
CFRelease(frame);
}
@end
关于ios - 如何绘制一个非矩形的 UITextView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9270997/
我有一个程序: [textView setFont:[UIFont fontWithName:@"ArialMT" size:20]]; [textVi
我正在 UITextViewDelegate 中处理代码。那里的每个函数都会接收接收事件的 UITextView 实例,例如: - (void)textViewDidBeginEditing:(UIT
我想在默认图像附件菜单选项(复制图像、保存到相机胶卷)中添加另一个菜单选项。请注意,如果 textView 未处于编辑模式,则当您长按嵌入在 UITextView 中的图像时会显示这些选项。 我已经尝
我想尝试禁用 UITextTapRecognizer,我认为它是 UITextView 的内置属性,因为我怀疑它会干扰我想要实现的点击手势识别器。这怎么可能? 感谢阅读! 最佳答案 我不确定这是否直接
首先我使用这个 UITextView 类 TextView 类.h @interface textViewClass : UITextView TextView 类.m @implementation
我正在开发一个应用程序,我需要为大文本放置一个 UITextView。我有一个尺寸为 180X89 的背景图像(与 UITextView 框架大小相同)。当将图像放在 UITextView 后面时,一
我只是想在 UITextView 中输入一些默认文本。有几个段落,我想用一个新行分隔这些段落。但是,当我按回车时,它只是完成了数据的输入。 在界面生成器中为 UITextView 创建默认文本时,如何
我的 iPhone 应用程序中有两个 UITextView。当用户选择一个 UITextView 时, Controller 会自动转到另一个 UITextView。我在我的项目中尝试了以下方式。但是
我正在使用 UITextView 并在我的代码中分配一些文本,但出于某种原因,它总是在 5-6 行之后开始。 UITextView 是一个 AttributedTextView 或允许属性字符串。
我有一个自定义 UIView,我想在其中布局一组 UITextView 以响应 AJAX 调用。所以我想我需要在代码中创建这些。我目前将位置设置为固定位置,但宁愿将其设置为比前一个位置低 20 像素。
我正在开发一个应用程序,它具有动态添加 TextView 的功能,在我们拖动它时增加和减小它的大小。 我可以根据字符数调整 TextView 大小,方法是将 TextView 的内容大小设置为 Tex
这可能是个愚蠢的问题。我正在尝试像所附图片一样设置左/右页边距。我通过在UIScrollView中添加UITextView成功实现了。但是,我几乎可以单独使用 UITextView 实现我想要的一切。
我有一个 UITextView。我正在根据方法 textViewDidChange 中文本的大小调整 TextView 的大小。这在我打字时工作正常。但是当我粘贴一些东西时,这不会接到电话。这是预期的
我正在开发一个基于消息的 iPhone 应用程序。我有一个屏幕可以回复收到的消息。此屏幕包含两个 UITextView,如 bottomTextView 和 topTextView。 topTextV
我用 View 制作了一个 UITextview 自定义控件。它工作正常。当我输入新行时, TextView 会随着 View (深蓝色)而增加。此后 TextViell 将以滚动的形式工作。 代码是
我想在用户完成编辑并点击屏幕上的其他地方后对我的 UITextView 执行一些处理。最佳做法是什么? 我几乎设法通过 func textViewDidEndEditing(textView: UIT
我想更改富 UITextView (iOS 6) 的部分或全部属性文本,并允许用户撤消更改。 看完NSUndoManager documentation ,我尝试了第一种方法: “Simple und
当我在 IOS7 模拟器上测试我的应用程序时。 有时我在使用 sizeToFit 时发现它很奇怪的 UITextView .sizeToFit后的框架看起来是对的,但文字只能部分显示,就像下面的照片一
我想要一个 tablew View ,其行为类似于 Apple 的 iPhone 联系人应用程序:一个 uitableviewcell,里面有一个 uitextview,这样当我在 uitextvie
我有一个可编辑的 UITextView,然后是一个仅供选择的 UITextView。当我在第一个 UITextView 中写东西时,有时我想引用另一个 UITextView 来复制文本,然后将其粘贴到
我是一名优秀的程序员,十分优秀!