Text add strikethrough and underline

This commit is contained in:
pengfei.zhou 2020-04-13 17:41:17 +08:00 committed by osborn
parent 8faf0e5d33
commit 8cb135b7b1
13 changed files with 151 additions and 15 deletions

View File

@ -143,6 +143,16 @@ public class TextNode extends ViewNode<TextView> {
}
view.setLineSpacing(DoricUtils.dp2px(prop.asNumber().toFloat()), 1);
break;
case "strikethrough":
if (prop.isBoolean()) {
view.getPaint().setStrikeThruText(prop.asBoolean().value());
}
break;
case "underline":
if (prop.isBoolean()) {
view.getPaint().setUnderlineText(prop.asBoolean().value());
}
break;
default:
super.blend(view, name, prop);
break;

View File

@ -46,6 +46,36 @@ class LayoutDemo extends Panel {
},
height: 50,
}),
image({
imageBase64: icon_refresh,
scaleType: ScaleType.ScaleAspectFit,
backgroundColor: Color.GRAY,
layoutConfig: {
widthSpec: LayoutSpec.FIT,
heightSpec: LayoutSpec.JUST,
},
height: 50,
}),
image({
imageBase64: icon_refresh,
scaleType: ScaleType.ScaleAspectFit,
backgroundColor: Color.GRAY,
layoutConfig: {
widthSpec: LayoutSpec.FIT,
heightSpec: LayoutSpec.JUST,
},
height: 50,
}),
image({
imageBase64: icon_refresh,
scaleType: ScaleType.ScaleAspectFit,
backgroundColor: Color.GRAY,
layoutConfig: {
widthSpec: LayoutSpec.FIT,
heightSpec: LayoutSpec.JUST,
},
height: 50,
}),
image({
imageBase64: icon_refresh,
scaleType: ScaleType.ScaleAspectFit,
@ -75,9 +105,10 @@ class LayoutDemo extends Panel {
flexConfig: {
flexDirection: FlexDirection.ROW,
width: 200,
height: 100,
height: 200,
flexWrap: Wrap.WRAP,
alignContent: Align.CENTER,
alignContent: Align.FLEX_END,
alignItems: Align.FLEX_START,
},
backgroundColor: Color.GRAY.alpha(0.1),
}

View File

@ -69,6 +69,18 @@ class TextDemo extends Panel {
(this as Text).textColor = Color.BLACK;
}
}),
text({
text: "This is strikethrough text.",
textSize: 20,
textColor: Color.RED,
strikethrough: true,
}),
text({
text: "This is underline text.",
textSize: 20,
textColor: Color.BLUE,
underline: true,
}),
],
{
space: 10,

View File

@ -42,6 +42,8 @@ - (void)drawTextInRect:(CGRect)rect {
@interface DoricTextNode ()
@property(nonatomic, strong) NSMutableParagraphStyle *paragraphStyle;
@property(nonatomic, assign) NSNumber *underline;
@property(nonatomic, assign) NSNumber *strikethrough;
@end
@implementation DoricTextNode
@ -53,12 +55,9 @@ - (UILabel *)build {
- (void)blendView:(UILabel *)view forPropName:(NSString *)name propValue:(id)prop {
if ([name isEqualToString:@"text"]) {
if (self.paragraphStyle) {
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:prop];
[attributedString addAttribute:NSParagraphStyleAttributeName value:self.paragraphStyle range:NSMakeRange(0, [attributedString length])];
view.attributedText = attributedString;
} else {
view.text = prop;
if (self.paragraphStyle) {
[self reloadParagraphStyle];
}
} else if ([name isEqualToString:@"textSize"]) {
UIFont *font = view.font;
@ -113,18 +112,46 @@ - (void)blendView:(UILabel *)view forPropName:(NSString *)name propValue:(id)pro
UIFont *font = [UIFont fontWithName:iconfont size:view.font.pointSize];
view.font = font;
} else if ([name isEqualToString:@"lineSpacing"]) {
self.paragraphStyle = [NSMutableParagraphStyle new];
[self.paragraphStyle setLineSpacing:[prop floatValue]];
[self.paragraphStyle setAlignment:view.textAlignment];
NSString *labelText = view.text ?: @"";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];
[attributedString addAttribute:NSParagraphStyleAttributeName value:self.paragraphStyle range:NSMakeRange(0, [labelText length])];
view.attributedText = attributedString;
[[self ensureParagraphStyle] also:^(NSMutableParagraphStyle *it) {
[it setLineSpacing:[prop floatValue]];
[self reloadParagraphStyle];
}];
} else if ([name isEqualToString:@"underline"]) {
[[self ensureParagraphStyle] also:^(NSMutableParagraphStyle *it) {
self.underline = prop;
[self reloadParagraphStyle];
}];
} else if ([name isEqualToString:@"strikethrough"]) {
[[self ensureParagraphStyle] also:^(NSMutableParagraphStyle *it) {
self.strikethrough = prop;
[self reloadParagraphStyle];
}];
} else {
[super blendView:view forPropName:name propValue:prop];
}
}
- (NSMutableParagraphStyle *)ensureParagraphStyle {
if (self.paragraphStyle == nil) {
self.paragraphStyle = [NSMutableParagraphStyle new];
self.paragraphStyle.alignment = self.view.textAlignment;
}
return self.paragraphStyle;
}
- (void)reloadParagraphStyle {
NSString *labelText = self.view.text ?: @"";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];
[attributedString addAttribute:NSParagraphStyleAttributeName value:self.paragraphStyle range:NSMakeRange(0, [labelText length])];
if (self.underline) {
[attributedString addAttribute:NSUnderlineStyleAttributeName value:self.underline range:NSMakeRange(0, [labelText length])];
}
if (self.strikethrough) {
[attributedString addAttribute:NSStrikethroughStyleAttributeName value:self.strikethrough range:NSMakeRange(0, [labelText length])];
}
self.view.attributedText = attributedString;
}
- (void)blend:(NSDictionary *)props {
[super blend:props];
[self.view.superview setNeedsLayout];

View File

@ -1682,6 +1682,14 @@ var Text = /** @class */ (function (_super) {
Property,
__metadata$3("design:type", Number)
], Text.prototype, "lineSpacing", void 0);
__decorate$3([
Property,
__metadata$3("design:type", Boolean)
], Text.prototype, "strikethrough", void 0);
__decorate$3([
Property,
__metadata$3("design:type", Boolean)
], Text.prototype, "underline", void 0);
return Text;
}(View));
function text(config) {

View File

@ -1250,6 +1250,14 @@ __decorate$3([
Property,
__metadata$3("design:type", Number)
], Text.prototype, "lineSpacing", void 0);
__decorate$3([
Property,
__metadata$3("design:type", Boolean)
], Text.prototype, "strikethrough", void 0);
__decorate$3([
Property,
__metadata$3("design:type", Boolean)
], Text.prototype, "underline", void 0);
function text(config) {
const ret = new Text;
ret.layoutConfig = layoutConfig().fit();

View File

@ -2709,6 +2709,14 @@ __decorate$3([
Property,
__metadata$3("design:type", Number)
], Text.prototype, "lineSpacing", void 0);
__decorate$3([
Property,
__metadata$3("design:type", Boolean)
], Text.prototype, "strikethrough", void 0);
__decorate$3([
Property,
__metadata$3("design:type", Boolean)
], Text.prototype, "underline", void 0);
function text(config) {
const ret = new Text;
ret.layoutConfig = layoutConfig().fit();

4
doric-js/index.d.ts vendored
View File

@ -484,6 +484,8 @@ declare module 'doric/lib/src/widget/text' {
maxWidth?: number;
maxHeight?: number;
lineSpacing?: number;
strikethrough?: boolean;
underline?: boolean;
}
export class Text extends View implements IText {
text?: string;
@ -496,6 +498,8 @@ declare module 'doric/lib/src/widget/text' {
maxWidth?: number;
maxHeight?: number;
lineSpacing?: number;
strikethrough?: boolean;
underline?: boolean;
}
export function text(config: IText): Text;
}

View File

@ -12,6 +12,8 @@ export interface IText extends IView {
maxWidth?: number;
maxHeight?: number;
lineSpacing?: number;
strikethrough?: boolean;
underline?: boolean;
}
export declare class Text extends View implements IText {
text?: string;
@ -24,5 +26,7 @@ export declare class Text extends View implements IText {
maxWidth?: number;
maxHeight?: number;
lineSpacing?: number;
strikethrough?: boolean;
underline?: boolean;
}
export declare function text(config: IText): Text;

View File

@ -68,6 +68,14 @@ __decorate([
Property,
__metadata("design:type", Number)
], Text.prototype, "lineSpacing", void 0);
__decorate([
Property,
__metadata("design:type", Boolean)
], Text.prototype, "strikethrough", void 0);
__decorate([
Property,
__metadata("design:type", Boolean)
], Text.prototype, "underline", void 0);
export function text(config) {
const ret = new Text;
ret.layoutConfig = layoutConfig().fit();

View File

@ -29,6 +29,8 @@ export interface IText extends IView {
maxWidth?: number
maxHeight?: number
lineSpacing?: number
strikethrough?: boolean
underline?: boolean
}
export class Text extends View implements IText {
@ -61,6 +63,12 @@ export class Text extends View implements IText {
@Property
lineSpacing?: number
@Property
strikethrough?: boolean
@Property
underline?: boolean
}
export function text(config: IText) {

View File

@ -2767,6 +2767,14 @@ __decorate$3([
Property,
__metadata$3("design:type", Number)
], Text.prototype, "lineSpacing", void 0);
__decorate$3([
Property,
__metadata$3("design:type", Boolean)
], Text.prototype, "strikethrough", void 0);
__decorate$3([
Property,
__metadata$3("design:type", Boolean)
], Text.prototype, "underline", void 0);
function text(config) {
const ret = new Text;
ret.layoutConfig = layoutConfig().fit();

File diff suppressed because one or more lines are too long