Browse Source

多行显示,行高的正确显示方式

Steven 2 months ago
parent
commit
87e18c116a

BIN
KulexiuForTeacher/KulexiuForTeacher/Assets.xcassets/Home/Income/income_vip.imageset/income_vip@2x.png


BIN
KulexiuForTeacher/KulexiuForTeacher/Assets.xcassets/Home/Income/income_vip.imageset/income_vip@3x.png


+ 5 - 2
KulexiuForTeacher/KulexiuForTeacher/Common/Base/AlertView/KSPublicAlertView.m

@@ -64,8 +64,11 @@
 }
 
 + (CGFloat)getBaseOffline:(UIFont *)font lineHeight:(CGFloat)lineHeight {
-    CGFloat baselineOffset = (lineHeight - font.lineHeight) / 4;
-    return baselineOffset;
+    CGFloat descender = fabs(font.descender); // descender 为负值,取绝对值
+    CGFloat ascender = font.ascender;
+    CGFloat fontHeight = ascender + descender;
+    CGFloat offset = (lineHeight - fontHeight) / 2.0; // 假设上下均等分布
+    return offset;
 }
 
 + (NSMutableParagraphStyle *)getParagraphStyleWithFont:(UIFont *)font lineHeight:(CGFloat)lineHeigh {

+ 10 - 1
KulexiuForTeacher/KulexiuForTeacher/Module/Chat/GroupNotice/View/GroupNoticeCell.m

@@ -59,7 +59,7 @@
             paragraphStyle.maximumLineHeight = 21;
             paragraphStyle.minimumLineHeight = 21;
             UIFont *font = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
-            CGFloat baseLineOffset = (21 - font.lineHeight) / 4;
+            CGFloat baseLineOffset = [GroupNoticeCell getBaseOffline:font lineHeight:21];
             NSMutableAttributedString *attrs = [[NSMutableAttributedString alloc] initWithString:model.content attributes:@{NSParagraphStyleAttributeName:paragraphStyle,NSFontAttributeName:font, NSForegroundColorAttributeName:HexRGB(0x777777), NSBaselineOffsetAttributeName:@(baseLineOffset)}];
             self.noticeContent.attributedText = attrs;
         }
@@ -71,6 +71,15 @@
     }
 }
 
++ (CGFloat)getBaseOffline:(UIFont *)font lineHeight:(CGFloat)lineHeight {
+    CGFloat descender = fabs(font.descender); // descender 为负值,取绝对值
+    CGFloat ascender = font.ascender;
+    CGFloat fontHeight = ascender + descender;
+    CGFloat offset = (lineHeight - fontHeight) / 2.0; // 假设上下均等分布
+    return offset;
+}
+
+
 - (IBAction)editAction:(UIButton *)sender {
     if (self.callback) {
         self.callback(self.sourceModel,sender);

+ 9 - 1
KulexiuForTeacher/KulexiuForTeacher/Module/Chat/GroupNotice/View/GroupNoticeDetailBodyView.m

@@ -48,7 +48,7 @@
             paragraphStyle.maximumLineHeight = 21;
             paragraphStyle.minimumLineHeight = 21;
             UIFont *font = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
-            CGFloat baseLineOffset = (21 - font.lineHeight) / 4;
+            CGFloat baseLineOffset = [GroupNoticeDetailBodyView getBaseOffline:font lineHeight:21];
             NSMutableAttributedString *attrs = [[NSMutableAttributedString alloc] initWithString:model.content attributes:@{NSParagraphStyleAttributeName:paragraphStyle,NSFontAttributeName:font, NSForegroundColorAttributeName:HexRGB(0x777777), NSBaselineOffsetAttributeName:@(baseLineOffset)}];
             self.noticeContent.attributedText = attrs;
         }
@@ -58,6 +58,14 @@
     }
 }
 
++ (CGFloat)getBaseOffline:(UIFont *)font lineHeight:(CGFloat)lineHeight {
+    CGFloat descender = fabs(font.descender); // descender 为负值,取绝对值
+    CGFloat ascender = font.ascender;
+    CGFloat fontHeight = ascender + descender;
+    CGFloat offset = (lineHeight - fontHeight) / 2.0; // 假设上下均等分布
+    return offset;
+}
+
 - (void)configWithSource:(id)source enable:(BOOL)canEdit callback:(EditCallback)callback {
     if (callback) {
         self.callback = callback;

+ 5 - 2
KulexiuForTeacher/KulexiuForTeacher/Module/Course/MyGroupCourse/View/GroupCourseGroupHeadView.m

@@ -105,8 +105,11 @@
 }
 
 + (CGFloat)getBaseOffline:(UIFont *)font lineHeight:(CGFloat)lineHeight {
-    CGFloat baselineOffset = (lineHeight - font.lineHeight) / 4;
-    return baselineOffset;
+    CGFloat descender = fabs(font.descender); // descender 为负值,取绝对值
+    CGFloat ascender = font.ascender;
+    CGFloat fontHeight = ascender + descender;
+    CGFloat offset = (lineHeight - fontHeight) / 2.0; // 假设上下均等分布
+    return offset;
 }
 
 + (NSMutableParagraphStyle *)getParagraphStyleWithFont:(UIFont *)font lineHeight:(CGFloat)lineHeigh {

+ 7 - 4
KulexiuForTeacher/KulexiuForTeacher/Module/Course/MyGroupCourse/View/GroupCourseListCell.m

@@ -74,8 +74,8 @@
     if ([NSString isEmptyString:planDesc]) {
         planDesc = @"暂无课程规划";
     }
-    NSMutableParagraphStyle *paragraphStyle = [GroupCourseListCell getParagraphStyleWithFont:[UIFont systemFontOfSize:13.0f] lineHeight:22.0f];
-    CGFloat baselineOffset = [GroupCourseListCell getBaseOffline:[UIFont systemFontOfSize:13.0f] lineHeight:22.0f];
+    NSMutableParagraphStyle *paragraphStyle = [GroupCourseListCell getParagraphStyleWithFont:[UIFont systemFontOfSize:13.0f] lineHeight:20.0f];
+    CGFloat baselineOffset = [GroupCourseListCell getBaseOffline:[UIFont systemFontOfSize:13.0f] lineHeight:20.0f];
     NSMutableAttributedString *attrs = [[NSMutableAttributedString alloc] initWithString:planDesc attributes:@{NSParagraphStyleAttributeName:paragraphStyle, NSBaselineOffsetAttributeName:@(baselineOffset),NSFontAttributeName:[UIFont systemFontOfSize:13.0f], NSForegroundColorAttributeName:HexRGB(0x777777)}];
     self.coursPlanLabel.attributedText = attrs;
 }
@@ -164,8 +164,11 @@
 }
 
 + (CGFloat)getBaseOffline:(UIFont *)font lineHeight:(CGFloat)lineHeight {
-    CGFloat baselineOffset = (lineHeight - font.lineHeight) / 4;
-    return baselineOffset;
+    CGFloat descender = fabs(font.descender); // descender 为负值,取绝对值
+    CGFloat ascender = font.ascender;
+    CGFloat fontHeight = ascender + descender;
+    CGFloat offset = (lineHeight - fontHeight) / 2.0; // 假设上下均等分布
+    return offset;
 }
 
 + (NSMutableParagraphStyle *)getParagraphStyleWithFont:(UIFont *)font lineHeight:(CGFloat)lineHeigh {

+ 1 - 1
KulexiuForTeacher/KulexiuForTeacher/Module/Course/MyGroupCourse/View/GroupCourseListCell.xib

@@ -183,7 +183,7 @@
                             <view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="qeE-Ft-83e">
                                 <rect key="frame" x="12" y="111" width="268" height="54"/>
                                 <subviews>
-                                    <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="严琦老师带您零基础学习大号,通过4节课的学习掌握大号演奏的基本方式,培养良好的吐息习惯。" textAlignment="natural" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="vGB-9D-omH">
+                                    <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="严琦老师带您零基础学习大号,通过4节课的学习掌握大号演奏的基本方式,培养良好的吐息习惯。" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="vGB-9D-omH">
                                         <rect key="frame" x="7" y="6" width="254" height="42"/>
                                         <fontDescription key="fontDescription" type="system" pointSize="13"/>
                                         <color key="textColor" red="0.46666666666666667" green="0.46666666666666667" blue="0.46666666666666667" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>

+ 5 - 2
KulexiuForTeacher/KulexiuForTeacher/Module/Course/View/CourseGroupView/CourseGroupPlanDescView.m

@@ -72,8 +72,11 @@
 
 
 + (CGFloat)getBaseOffline:(UIFont *)font lineHeight:(CGFloat)lineHeight {
-    CGFloat baselineOffset = (lineHeight - font.lineHeight) / 4;
-    return baselineOffset;
+    CGFloat descender = fabs(font.descender); // descender 为负值,取绝对值
+    CGFloat ascender = font.ascender;
+    CGFloat fontHeight = ascender + descender;
+    CGFloat offset = (lineHeight - fontHeight) / 2.0; // 假设上下均等分布
+    return offset;
 }
 
 + (NSMutableParagraphStyle *)getParagraphStyleWithFont:(UIFont *)font lineHeight:(CGFloat)lineHeigh {

+ 5 - 2
KulexiuForTeacher/KulexiuForTeacher/Module/Home/MyCourse/View/CourseGroupAlertView/CourseDescAlertCell.m

@@ -33,8 +33,11 @@
 }
 
 - (CGFloat)getBaseOffline:(UIFont *)font lineHeight:(CGFloat)lineHeight {
-    CGFloat baselineOffset = (lineHeight - font.lineHeight) / 4;
-    return baselineOffset;
+    CGFloat descender = fabs(font.descender); // descender 为负值,取绝对值
+    CGFloat ascender = font.ascender;
+    CGFloat fontHeight = ascender + descender;
+    CGFloat offset = (lineHeight - fontHeight) / 2.0; // 假设上下均等分布
+    return offset;
 }
 
 - (NSMutableParagraphStyle *)getParagraphStyleWithFont:(UIFont *)font lineHeight:(CGFloat)lineHeigh {

+ 5 - 2
KulexiuForTeacher/KulexiuForTeacher/Module/Mine/MinePage/View/HeadView/MinePageTopView.m

@@ -159,8 +159,11 @@
 }
 
 - (CGFloat)getBaseOffline:(UIFont *)font lineHeight:(CGFloat)lineHeight {
-    CGFloat baselineOffset = (lineHeight - font.lineHeight) / 4;
-    return baselineOffset;
+    CGFloat descender = fabs(font.descender); // descender 为负值,取绝对值
+    CGFloat ascender = font.ascender;
+    CGFloat fontHeight = ascender + descender;
+    CGFloat offset = (lineHeight - fontHeight) / 2.0; // 假设上下均等分布
+    return offset;
 }
 
 - (NSMutableParagraphStyle *)getParagraphStyleWithFont:(UIFont *)font lineHeight:(CGFloat)lineHeigh {

+ 5 - 2
KulexiuForTeacher/KulexiuForTeacher/Module/Mine/MinePage/View/MinePageTipsView/MinePageCourseTipsView.m

@@ -57,8 +57,11 @@
 }
 
 - (CGFloat)getBaseOffline:(UIFont *)font lineHeight:(CGFloat)lineHeight {
-    CGFloat baselineOffset = (lineHeight - font.lineHeight) / 4;
-    return baselineOffset;
+    CGFloat descender = fabs(font.descender); // descender 为负值,取绝对值
+    CGFloat ascender = font.ascender;
+    CGFloat fontHeight = ascender + descender;
+    CGFloat offset = (lineHeight - fontHeight) / 2.0; // 假设上下均等分布
+    return offset;
 }
 
 - (NSMutableParagraphStyle *)getParagraphStyleWithFont:(UIFont *)font lineHeight:(CGFloat)lineHeigh {