feat:add Text lineSpacing

This commit is contained in:
pengfei.zhou 2020-03-24 11:06:30 +08:00 committed by osborn
parent a25aedcc6f
commit 0cfbef67a2
12 changed files with 75 additions and 5 deletions

View File

@ -23,6 +23,7 @@ import android.widget.TextView;
import com.github.pengfeizhou.jscore.JSValue; import com.github.pengfeizhou.jscore.JSValue;
import pub.doric.Doric;
import pub.doric.DoricContext; import pub.doric.DoricContext;
import pub.doric.extension.bridge.DoricPlugin; import pub.doric.extension.bridge.DoricPlugin;
import pub.doric.utils.DoricUtils; import pub.doric.utils.DoricUtils;
@ -51,6 +52,7 @@ public class TextNode extends ViewNode<TextView> {
protected void blend(TextView view, String name, JSValue prop) { protected void blend(TextView view, String name, JSValue prop) {
switch (name) { switch (name) {
case "text": case "text":
view.setLineSpacing(0, 1);
view.setText(prop.asString().toString()); view.setText(prop.asString().toString());
break; break;
case "textSize": case "textSize":
@ -63,7 +65,11 @@ public class TextNode extends ViewNode<TextView> {
view.setGravity(prop.asNumber().toInt() | Gravity.CENTER_VERTICAL); view.setGravity(prop.asNumber().toInt() | Gravity.CENTER_VERTICAL);
break; break;
case "maxLines": case "maxLines":
view.setMaxLines(prop.asNumber().toInt()); int line = prop.asNumber().toInt();
if (line <= 0) {
line = Integer.MAX_VALUE;
}
view.setMaxLines(line);
break; break;
case "fontStyle": case "fontStyle":
if (prop.isString()) { if (prop.isString()) {
@ -97,6 +103,9 @@ public class TextNode extends ViewNode<TextView> {
case "maxHeight": case "maxHeight":
view.setMaxHeight(DoricUtils.dp2px(prop.asNumber().toFloat())); view.setMaxHeight(DoricUtils.dp2px(prop.asNumber().toFloat()));
break; break;
case "lineSpacing":
view.setLineSpacing(DoricUtils.dp2px(prop.asNumber().toFloat()), 1);
break;
default: default:
super.blend(view, name, prop); super.blend(view, name, prop);
break; break;

View File

@ -1,4 +1,4 @@
import { Panel, Group, scroller, vlayout, layoutConfig, LayoutSpec, Input, Gravity, log, input, text } from "doric"; import { Panel, Group, scroller, vlayout, layoutConfig, LayoutSpec, Input, Gravity, log, input, text, Color } from "doric";
import { title } from "./utils"; import { title } from "./utils";
@Entry @Entry
class TextDemo extends Panel { class TextDemo extends Panel {
@ -54,6 +54,17 @@ class TextDemo extends Panel {
textSize: 30, textSize: 30,
font: 'iconfont' font: 'iconfont'
}), }),
text({
text: "This is line Spaceing 0,\nSecond line",
maxLines: 0,
}),
text({
text: "This is line Spaceing 40,\nSecond line",
maxLines: 0,
lineSpacing: 40,
textColor: Color.RED,
textAlignment: Gravity.Right,
}),
], ],
{ {
space: 10, space: 10,

View File

@ -49,6 +49,10 @@ - (CGSize)measureSize:(CGSize)targetSize {
} }
@end @end
@interface DoricTextNode ()
@property(nonatomic, strong) NSMutableParagraphStyle *paragraphStyle;
@end
@implementation DoricTextNode @implementation DoricTextNode
- (UILabel *)build { - (UILabel *)build {
return [[[DoricTextView alloc] init] also:^(UILabel *it) { return [[[DoricTextView alloc] init] also:^(UILabel *it) {
@ -58,7 +62,13 @@ - (UILabel *)build {
- (void)blendView:(UILabel *)view forPropName:(NSString *)name propValue:(id)prop { - (void)blendView:(UILabel *)view forPropName:(NSString *)name propValue:(id)prop {
if ([name isEqualToString:@"text"]) { if ([name isEqualToString:@"text"]) {
view.text = prop; 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;
}
} else if ([name isEqualToString:@"textSize"]) { } else if ([name isEqualToString:@"textSize"]) {
UIFont *font = view.font; UIFont *font = view.font;
if (font) { if (font) {
@ -76,7 +86,11 @@ - (void)blendView:(UILabel *)view forPropName:(NSString *)name propValue:(id)pro
} else if ((gravity & RIGHT) == RIGHT) { } else if ((gravity & RIGHT) == RIGHT) {
alignment = NSTextAlignmentRight; alignment = NSTextAlignmentRight;
} }
view.textAlignment = alignment; if (self.paragraphStyle) {
self.paragraphStyle.alignment = alignment;
} else {
view.textAlignment = alignment;
}
} else if ([name isEqualToString:@"maxLines"]) { } else if ([name isEqualToString:@"maxLines"]) {
view.numberOfLines = [prop integerValue]; view.numberOfLines = [prop integerValue];
} else if ([name isEqualToString:@"fontStyle"]) { } else if ([name isEqualToString:@"fontStyle"]) {
@ -108,6 +122,14 @@ - (void)blendView:(UILabel *)view forPropName:(NSString *)name propValue:(id)pro
NSString *iconfont = prop; NSString *iconfont = prop;
UIFont *font = [UIFont fontWithName:iconfont size:view.font.pointSize]; UIFont *font = [UIFont fontWithName:iconfont size:view.font.pointSize];
view.font = font; 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;
} else { } else {
[super blendView:view forPropName:name propValue:prop]; [super blendView:view forPropName:name propValue:prop];
} }

View File

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

View File

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

View File

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

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

@ -474,6 +474,7 @@ declare module 'doric/lib/src/widget/text' {
font?: string; font?: string;
maxWidth?: number; maxWidth?: number;
maxHeight?: number; maxHeight?: number;
lineSpacing?: number;
} }
export class Text extends View implements IText { export class Text extends View implements IText {
text?: string; text?: string;
@ -485,6 +486,7 @@ declare module 'doric/lib/src/widget/text' {
font?: string; font?: string;
maxWidth?: number; maxWidth?: number;
maxHeight?: number; maxHeight?: number;
lineSpacing?: number;
} }
export function text(config: IText): Text; export function text(config: IText): Text;
} }

View File

@ -11,6 +11,7 @@ export interface IText extends IView {
font?: string; font?: string;
maxWidth?: number; maxWidth?: number;
maxHeight?: number; maxHeight?: number;
lineSpacing?: number;
} }
export declare class Text extends View implements IText { export declare class Text extends View implements IText {
text?: string; text?: string;
@ -22,5 +23,6 @@ export declare class Text extends View implements IText {
font?: string; font?: string;
maxWidth?: number; maxWidth?: number;
maxHeight?: number; maxHeight?: number;
lineSpacing?: number;
} }
export declare function text(config: IText): Text; export declare function text(config: IText): Text;

View File

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

View File

@ -28,6 +28,7 @@ export interface IText extends IView {
font?: string font?: string
maxWidth?: number maxWidth?: number
maxHeight?: number maxHeight?: number
lineSpacing?: number
} }
export class Text extends View implements IText { export class Text extends View implements IText {
@ -57,6 +58,9 @@ export class Text extends View implements IText {
@Property @Property
maxHeight?: number maxHeight?: number
@Property
lineSpacing?: number
} }
export function text(config: IText) { export function text(config: IText) {

View File

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

File diff suppressed because one or more lines are too long