Bladeren bron

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

Steven 3 maanden geleden
bovenliggende
commit
acbd79cf45

+ 9 - 1
KulexiuForStudent/KulexiuForStudent/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,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;
+}
+
 - (IBAction)editAction:(UIButton *)sender {
     if (self.callback) {
         self.callback(self.sourceModel,sender);

+ 9 - 1
KulexiuForStudent/KulexiuForStudent/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
KulexiuForStudent/KulexiuForStudent/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
KulexiuForStudent/KulexiuForStudent/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;
 }
@@ -146,8 +146,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
KulexiuForStudent/KulexiuForStudent/Module/Course/MyGroupCourse/View/MyGroupCourseGroupTipsView.m

@@ -35,8 +35,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
KulexiuForStudent/KulexiuForStudent/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
KulexiuForStudent/KulexiuForStudent/Module/Home/ArrangeCourse/View/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 {